2 Copyright (C) 2007, 2010, 2011
3 The Free Software Foundation, Inc.
6 Daniel Borca <dborca@yahoo.com>, 2007
7 Slava Zanko <slavazanko@gmail.com>, 2010
8 Andrew Borodin <aborodin@vmail.ru>, 2010
9 Ilia Maslakov <il.smind@gmail.com>, 2010
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
34 #include <sys/types.h>
37 #include "lib/global.h"
38 #include "lib/tty/tty.h"
39 #include "lib/tty/color.h"
40 #include "lib/tty/key.h"
41 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
42 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
44 #include "lib/widget.h"
45 #include "lib/strutil.h"
47 #include "lib/charsets.h"
49 #include "lib/event.h" /* mc_event_raise() */
51 #include "src/filemanager/cmd.h" /* do_edit_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/history.h"
58 #include "src/selcodepage.h"
64 /*** global variables ****************************************************************************/
66 /*** file scope macro definitions ****************************************************************/
68 #define g_array_foreach(a, TP, cbf) \
70 size_t g_array_foreach_i;\
71 TP *g_array_foreach_var = NULL; \
72 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
74 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
75 (*cbf) (g_array_foreach_var); \
79 #define FILE_READ_BUF 4096
80 #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 * @returns 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
= vfs_path_to_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 * @returns 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 * @returns 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 * @returns file structure
260 fd
= open_temp (&fs
->data
);
268 fs
->flags
= FILE_FLAG_TEMP
;
272 /* --------------------------------------------------------------------------------------------- */
275 * Open a binary file in specified mode.
277 * @param filename file name
278 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
280 * @returns file structure
284 f_open (const char *filename
, int flags
)
293 fd
= open (filename
, flags
);
304 /* --------------------------------------------------------------------------------------------- */
307 * Read a line of bytes from file until newline or EOF.
308 * @note does not stop on null-byte
309 * @note buf will not be null-terminated
311 * @param buf destination buffer
312 * @param size size of buffer
313 * @param fs file structure
315 * @returns number of bytes read
320 f_gets (char *buf
, size_t size
, FBUF
* fs
)
329 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
337 if (j
== size
|| stop
)
341 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
348 /* --------------------------------------------------------------------------------------------- */
352 * @note avoids thrashing read cache when possible
354 * @param fs file structure
356 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
358 * @returns position in file, starting from begginning
363 f_seek (FBUF
* fs
, off_t off
, int whence
)
367 if (fs
->len
&& whence
!= SEEK_END
)
369 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
372 if (whence
== SEEK_CUR
)
375 off
+= rv
- fs
->len
+ fs
->pos
;
377 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
379 fs
->pos
= fs
->len
+ off
- rv
;
385 rv
= lseek (fs
->fd
, off
, whence
);
391 /* --------------------------------------------------------------------------------------------- */
394 * Seek to the beginning of file, thrashing read cache.
396 * @param fs file structure
398 * @returns 0 if success, non-zero on error
406 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
412 /* --------------------------------------------------------------------------------------------- */
415 * Write bytes to file.
416 * @note thrashes read cache
418 * @param fs file structure
419 * @param buf source buffer
420 * @param size size of buffer
422 * @returns number of written bytes, -1 on error
427 f_write (FBUF
* fs
, const char *buf
, size_t size
)
431 rv
= write (fs
->fd
, buf
, size
);
437 /* --------------------------------------------------------------------------------------------- */
440 * Truncate file to the current position.
441 * @note thrashes read cache
443 * @param fs file structure
445 * @returns current file size on success, negative on error
454 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
459 rv
= ftruncate (fs
->fd
, off
);
468 /* --------------------------------------------------------------------------------------------- */
472 * @note if this is temporary file, it is deleted
474 * @param fs file structure
475 * @returns 0 on success, non-zero on error
489 /* --------------------------------------------------------------------------------------------- */
492 * Create pipe stream to process.
494 * @param cmd shell command line
495 * @param flags open mode, either O_RDONLY or O_WRONLY
497 * @returns file structure
501 p_open (const char *cmd
, int flags
)
505 const char *type
= NULL
;
507 if (flags
== O_RDONLY
)
509 else if (flags
== O_WRONLY
)
519 f
= popen (cmd
, type
);
531 /* --------------------------------------------------------------------------------------------- */
536 * @param fs structure
537 * @returns 0 on success, non-zero on error
545 rv
= pclose (fs
->data
);
550 /* --------------------------------------------------------------------------------------------- */
552 * Get one char (byte) from string
554 * @param char * str, gboolean * result
555 * @returns int as character or 0 and result == FALSE if fail
559 dview_get_byte (char *str
, gboolean
* result
)
567 return (unsigned char) *str
;
570 /* --------------------------------------------------------------------------------------------- */
573 * Get utf multibyte char from string
575 * @param char * str, int * char_width, gboolean * result
576 * @returns int as utf character or 0 and result == FALSE if fail
581 dview_get_utf (char *str
, int *char_width
, gboolean
* result
)
585 gchar
*next_ch
= NULL
;
596 res
= g_utf8_get_char_validated (str
, -1);
603 /* Calculate UTF-8 char width */
604 next_ch
= g_utf8_next_char (str
);
606 width
= next_ch
- str
;
614 /* --------------------------------------------------------------------------------------------- */
617 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
621 if (text
== NULL
|| text
[0] == '\0')
624 if (g_utf8_validate (text
, -1, NULL
))
625 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
629 char *tmpbuf
, *buffer
;
631 buffer
= tmpbuf
= g_strdup (text
);
632 while (tmpbuf
[0] != '\0')
634 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
635 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
636 tmpbuf
= g_utf8_next_char (tmpbuf
);
643 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
646 return max (length
, (size_t) result
);
649 /* --------------------------------------------------------------------------------------------- */
651 /* diff parse *************************************************************** */
654 * Read decimal number from string.
656 * @param[in,out] str string to parse
657 * @param[out] n extracted number
658 * @returns 0 if success, otherwise non-zero
662 scan_deci (const char **str
, int *n
)
664 const char *p
= *str
;
668 *n
= strtol (p
, &q
, 10);
669 if (errno
!= 0 || p
== q
)
675 /* --------------------------------------------------------------------------------------------- */
678 * Parse line for diff statement.
680 * @param p string to parse
681 * @param ops list of diff statements
682 * @returns 0 if success, otherwise non-zero
686 scan_line (const char *p
, GArray
* ops
)
695 /* handle the following cases:
697 * NUM[,NUM]cNUM[,NUM]
699 * where NUM is a positive integer
702 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
710 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
722 else if (cmd
!= 'c' && cmd
!= 'd')
725 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
733 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
739 if (cmd
== 'd' && range
!= 0)
747 g_array_append_val (ops
, op
);
751 /* --------------------------------------------------------------------------------------------- */
754 * Parse diff output and extract diff statements.
756 * @param f stream to read from
757 * @param ops list of diff statements to fill
758 * @returns positive number indicating number of hunks, otherwise negative
762 scan_diff (FBUF
* f
, GArray
* ops
)
767 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
769 if (isdigit (buf
[0]))
771 if (buf
[sz
- 1] != '\n')
775 if (scan_line (buf
, ops
) != 0)
781 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
788 /* --------------------------------------------------------------------------------------------- */
791 * Invoke diff and extract diff statements.
793 * @param args extra arguments to be passed to diff
794 * @param extra more arguments to be passed to diff
795 * @param file1 first file to compare
796 * @param file2 second file to compare
797 * @param ops list of diff statements to fill
799 * @returns positive number indicating number of hunks, otherwise negative
803 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
806 static const char *opt
=
807 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
808 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
809 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
810 " --unchanged-group-format=''";
817 cmd
= g_strdup_printf ("diff %s %s %s \"%s\" \"%s\"", args
, extra
, opt
, file1
, file2
);
821 f
= p_open (cmd
, O_RDONLY
);
827 rv
= scan_diff (f
, ops
);
830 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
836 /* --------------------------------------------------------------------------------------------- */
839 * Reparse and display file according to diff statements.
841 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
842 * @param filename file name to display
843 * @param ops list of diff statements
844 * @param printer printf-like function to be used for displaying
845 * @param ctx printer context
847 * @returns 0 if success, otherwise non-zero
851 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
864 f
= f_open (filename
, O_RDONLY
);
880 #define T1 a[ ord^1 ][0]
881 #define T2 a[ ord^1 ][1]
882 for (i
= 0; i
< ops
->len
; i
++)
886 op
= &g_array_index (ops
, DIFFCMD
, i
);
887 n
= op
->F1
- (op
->cmd
!= add_cmd
);
889 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
892 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
894 while (buf
[sz
- 1] != '\n')
896 sz
= f_gets (buf
, sizeof (buf
), f
);
899 printer (ctx
, 0, 0, 0, 1, "\n");
902 printer (ctx
, 0, 0, 0, sz
, buf
);
910 if (op
->cmd
== add_cmd
)
912 n
= op
->T2
- op
->T1
+ 1;
915 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
920 if (op
->cmd
== del_cmd
)
922 n
= op
->F2
- op
->F1
+ 1;
923 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
926 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
928 while (buf
[sz
- 1] != '\n')
930 sz
= f_gets (buf
, sizeof (buf
), f
);
933 printer (ctx
, 0, 0, 0, 1, "\n");
936 printer (ctx
, 0, 0, 0, sz
, buf
);
948 n
= op
->F2
- op
->F1
+ 1;
949 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
952 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
954 while (buf
[sz
- 1] != '\n')
956 sz
= f_gets (buf
, sizeof (buf
), f
);
959 printer (ctx
, 0, 0, 0, 1, "\n");
962 printer (ctx
, 0, 0, 0, sz
, buf
);
971 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
974 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
984 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
987 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
989 while (buf
[sz
- 1] != '\n')
991 sz
= f_gets (buf
, sizeof (buf
), f
);
994 printer (ctx
, 0, 0, 0, 1, "\n");
997 printer (ctx
, 0, 0, 0, sz
, buf
);
1010 /* --------------------------------------------------------------------------------------------- */
1012 /* horizontal diff ********************************************************** */
1015 * Longest common substring.
1017 * @param s first string
1018 * @param m length of first string
1019 * @param t second string
1020 * @param n length of second string
1021 * @param ret list of offsets for longest common substrings inside each string
1022 * @param min minimum length of common substrings
1024 * @returns 0 if success, nonzero otherwise
1028 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1034 if (m
< min
|| n
< min
)
1036 /* XXX early culling */
1040 Lprev
= g_try_new0 (int, n
+ 1);
1044 Lcurr
= g_try_new0 (int, n
+ 1);
1051 for (i
= 0; i
< m
; i
++)
1058 #ifdef USE_MEMSET_IN_LCS
1059 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1061 for (j
= 0; j
< n
; j
++)
1063 #ifndef USE_MEMSET_IN_LCS
1075 g_array_set_size (ret
, 0);
1077 if (z
== v
&& z
>= min
)
1085 for (k
= 0; k
< ret
->len
; k
++)
1087 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1088 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1097 g_array_append_val (ret
, p2
);
1109 /* --------------------------------------------------------------------------------------------- */
1112 * Scan recursively for common substrings and build ranges.
1114 * @param s first string
1115 * @param t second string
1116 * @param bracket current limits for both of the strings
1117 * @param min minimum length of common substrings
1118 * @param hdiff list of horizontal diff ranges to fill
1119 * @param depth recursion depth
1121 * @returns 0 if success, nonzero otherwise
1125 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1136 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1140 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1141 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1145 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1148 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1149 b
[DIFF_LEFT
].len
= (*data
)[0];
1150 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1151 b
[DIFF_RIGHT
].len
= (*data
)[1];
1152 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1155 for (k
= 0; k
< ret
->len
- 1; k
++)
1157 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1158 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1159 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1160 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1161 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1162 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1163 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1166 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1167 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1168 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1169 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1170 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1171 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1174 g_array_free (ret
, TRUE
);
1179 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1180 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1181 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1182 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1183 g_array_append_val (hdiff
, p
);
1188 /* --------------------------------------------------------------------------------------------- */
1191 * Build list of horizontal diff ranges.
1193 * @param s first string
1194 * @param m length of first string
1195 * @param t second string
1196 * @param n length of second string
1197 * @param min minimum length of common substrings
1198 * @param hdiff list of horizontal diff ranges to fill
1199 * @param depth recursion depth
1201 * @returns 0 if success, nonzero otherwise
1205 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1210 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1211 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1213 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1216 b
[DIFF_LEFT
].off
= i
;
1217 b
[DIFF_LEFT
].len
= m
- i
;
1218 b
[DIFF_RIGHT
].off
= i
;
1219 b
[DIFF_RIGHT
].len
= n
- i
;
1221 /* smartscan (multiple horizontal diff) */
1222 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1225 /* --------------------------------------------------------------------------------------------- */
1227 /* read line **************************************************************** */
1230 * Check if character is inside horizontal diff limits.
1232 * @param k rank of character inside line
1233 * @param hdiff horizontal diff structure
1234 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1236 * @returns TRUE if inside hdiff limits, FALSE otherwise
1240 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1245 for (i
= 0; i
< hdiff
->len
; i
++)
1249 b
= &g_array_index (hdiff
, BRACKET
, i
);
1250 start
= (*b
)[ord
].off
;
1251 end
= start
+ (*b
)[ord
].len
;
1252 if (k
>= start
&& k
< end
)
1258 /* --------------------------------------------------------------------------------------------- */
1261 * Copy 'src' to 'dst' expanding tabs.
1262 * @note The procedure returns when all bytes are consumed from 'src'
1264 * @param dst destination buffer
1265 * @param src source buffer
1266 * @param srcsize size of src buffer
1267 * @param base virtual base of this string, needed to calculate tabs
1268 * @param ts tab size
1270 * @returns new virtual base
1274 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1278 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1285 j
= TAB_SKIP (ts
, i
+ base
);
1295 /* --------------------------------------------------------------------------------------------- */
1298 * Copy 'src' to 'dst' expanding tabs.
1300 * @param dst destination buffer
1301 * @param dstsize size of dst buffer
1302 * @param[in,out] _src source buffer
1303 * @param srcsize size of src buffer
1304 * @param base virtual base of this string, needed to calculate tabs
1305 * @param ts tab size
1307 * @returns new virtual base
1309 * @note The procedure returns when all bytes are consumed from 'src'
1310 * or 'dstsize' bytes are written to 'dst'
1311 * @note Upon return, 'src' points to the first unwritten character in source
1315 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1318 const char *src
= *_src
;
1320 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1327 j
= TAB_SKIP (ts
, i
+ base
);
1328 if (j
> dstsize
- i
)
1340 /* --------------------------------------------------------------------------------------------- */
1343 * Read line from memory, converting tabs to spaces and padding with spaces.
1345 * @param src buffer to read from
1346 * @param srcsize size of src buffer
1347 * @param dst buffer to read to
1348 * @param dstsize size of dst buffer, excluding trailing null
1349 * @param skip number of characters to skip
1350 * @param ts tab size
1351 * @param show_cr show trailing carriage return as ^M
1353 * @returns negative on error, otherwise number of bytes except padding
1357 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1367 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1373 j
= TAB_SKIP (ts
, i
+ base
);
1379 else if (dstsize
!= 0)
1386 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1388 if (skip
== 0 && show_cr
)
1411 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1414 if (!g_unichar_isprint (utf_ch
))
1425 while (dstsize
!= 0)
1434 /* --------------------------------------------------------------------------------------------- */
1437 * Read line from memory and build attribute array.
1439 * @param src buffer to read from
1440 * @param srcsize size of src buffer
1441 * @param dst buffer to read to
1442 * @param dstsize size of dst buffer, excluding trailing null
1443 * @param skip number of characters to skip
1444 * @param ts tab size
1445 * @param show_cr show trailing carriage return as ^M
1446 * @param hdiff horizontal diff structure
1447 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1448 * @param att buffer of attributes
1450 * @returns negative on error, otherwise number of bytes except padding
1454 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1455 GArray
* hdiff
, diff_place_t ord
, char *att
)
1465 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1471 j
= TAB_SKIP (ts
, i
+ base
);
1477 else if (dstsize
!= 0)
1480 *att
++ = is_inside (k
, hdiff
, ord
);
1485 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1487 if (skip
== 0 && show_cr
)
1492 *att
++ = is_inside (k
, hdiff
, ord
);
1494 *att
++ = is_inside (k
, hdiff
, ord
);
1500 *att
++ = is_inside (k
, hdiff
, ord
);
1513 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1516 if (!g_unichar_isprint (utf_ch
))
1522 *att
++ = is_inside (k
, hdiff
, ord
);
1528 while (dstsize
!= 0)
1538 /* --------------------------------------------------------------------------------------------- */
1541 * Read line from file, converting tabs to spaces and padding with spaces.
1543 * @param f file stream to read from
1544 * @param off offset of line inside file
1545 * @param dst buffer to read to
1546 * @param dstsize size of dst buffer, excluding trailing null
1547 * @param skip number of characters to skip
1548 * @param ts tab size
1549 * @param show_cr show trailing carriage return as ^M
1551 * @returns negative on error, otherwise number of bytes except padding
1555 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1558 int old_base
= base
;
1559 size_t amount
= dstsize
;
1560 size_t useful
, offset
;
1564 const char *q
= NULL
;
1565 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1566 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1568 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1570 /* abnormal, but avoid buffer overflow */
1571 memset (dst
, ' ', dstsize
);
1572 dst
[dstsize
] = '\0';
1576 f_seek (f
, off
, SEEK_SET
);
1581 sz
= f_gets (tmp
, amount
, f
);
1585 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1586 if (cvt
[base
- old_base
- 1] == '\n')
1588 q
= &cvt
[base
- old_base
- 1];
1589 base
= old_base
+ q
- cvt
+ 1;
1596 memset (dst
, ' ', dstsize
);
1597 dst
[dstsize
] = '\0';
1601 useful
= base
- skip
;
1602 offset
= skip
- old_base
;
1604 if (useful
<= dstsize
)
1607 memmove (dst
, cvt
+ offset
, useful
);
1611 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1614 const char *ptr
= tmp
;
1616 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1625 memmove (dst
, cvt
+ offset
, dstsize
);
1627 lastch
= cvt
[offset
+ dstsize
];
1631 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1633 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1637 if (i
+ 1 < dstsize
)
1651 for (; i
< dstsize
; i
++)
1657 /* --------------------------------------------------------------------------------------------- */
1658 /* diff printers et al ****************************************************** */
1661 cc_free_elt (void *elt
)
1669 /* --------------------------------------------------------------------------------------------- */
1672 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1674 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1675 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1685 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1687 if (sz
!= 0 && str
[sz
- 1] == '\n')
1690 p
.p
= g_strndup (str
, sz
);
1693 g_array_append_val (a
, p
);
1695 else if (dsrc
== DATA_SRC_MEM
)
1699 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1700 if (sz
!= 0 && str
[sz
- 1] == '\n')
1707 new_size
= p
->u
.len
+ sz
;
1708 q
= g_realloc (p
->p
, new_size
);
1709 memcpy (q
+ p
->u
.len
, str
, sz
);
1714 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1716 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1717 f_write (f
, str
, sz
);
1722 /* --------------------------------------------------------------------------------------------- */
1725 redo_diff (WDiff
* dview
)
1727 FBUF
*const *f
= dview
->f
;
1735 if (dview
->opt
.quality
== 2)
1736 strcat (extra
, " -d");
1737 if (dview
->opt
.quality
== 1)
1738 strcat (extra
, " --speed-large-files");
1739 if (dview
->opt
.strip_trailing_cr
)
1740 strcat (extra
, " --strip-trailing-cr");
1741 if (dview
->opt
.ignore_tab_expansion
)
1742 strcat (extra
, " -E");
1743 if (dview
->opt
.ignore_space_change
)
1744 strcat (extra
, " -b");
1745 if (dview
->opt
.ignore_all_space
)
1746 strcat (extra
, " -w");
1747 if (dview
->opt
.ignore_case
)
1748 strcat (extra
, " -i");
1750 if (dview
->dsrc
!= DATA_SRC_MEM
)
1752 f_reset (f
[DIFF_LEFT
]);
1753 f_reset (f
[DIFF_RIGHT
]);
1756 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1757 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1761 g_array_free (ops
, TRUE
);
1765 ctx
.dsrc
= dview
->dsrc
;
1768 ctx
.a
= dview
->a
[DIFF_LEFT
];
1769 ctx
.f
= f
[DIFF_LEFT
];
1770 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1772 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1773 ctx
.f
= f
[DIFF_RIGHT
];
1774 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1777 g_array_free (ops
, TRUE
);
1779 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1782 if (dview
->dsrc
== DATA_SRC_TMP
)
1784 f_trunc (f
[DIFF_LEFT
]);
1785 f_trunc (f
[DIFF_RIGHT
]);
1788 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1790 dview
->hdiff
= g_ptr_array_new ();
1791 if (dview
->hdiff
!= NULL
)
1797 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1801 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1802 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1803 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1805 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1811 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1815 g_array_free (h
, TRUE
);
1820 g_ptr_array_add (dview
->hdiff
, h
);
1827 /* --------------------------------------------------------------------------------------------- */
1830 destroy_hdiff (WDiff
* dview
)
1832 if (dview
->hdiff
!= NULL
)
1837 len
= dview
->a
[DIFF_LEFT
]->len
;
1839 for (i
= 0; i
< len
; i
++)
1843 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1845 g_array_free (h
, TRUE
);
1847 g_ptr_array_free (dview
->hdiff
, TRUE
);
1848 dview
->hdiff
= NULL
;
1851 mc_search_free (dview
->search
.handle
);
1852 dview
->search
.handle
= NULL
;
1853 g_free (dview
->search
.last_string
);
1854 dview
->search
.last_string
= NULL
;
1857 /* --------------------------------------------------------------------------------------------- */
1858 /* stuff ******************************************************************** */
1861 get_digits (unsigned int n
)
1870 /* --------------------------------------------------------------------------------------------- */
1873 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1885 p
= &g_array_index (a
, DIFFLN
, pos
);
1891 for (n
= pos
; n
> 0; n
--)
1897 *lineofs
= pos
- n
+ 1;
1905 /* --------------------------------------------------------------------------------------------- */
1908 calc_nwidth (const GArray
** const a
)
1913 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1914 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1917 return get_digits (l1
);
1920 /* --------------------------------------------------------------------------------------------- */
1923 find_prev_hunk (const GArray
* a
, int pos
)
1926 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1928 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1930 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1932 if (pos
> 0 && (size_t) pos
< a
->len
)
1935 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1937 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1944 /* --------------------------------------------------------------------------------------------- */
1947 find_next_hunk (const GArray
* a
, size_t pos
)
1949 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1951 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1956 /* --------------------------------------------------------------------------------------------- */
1958 * Find start and end lines of the current hunk.
1960 * @param dview WDiff widget
1961 * @returns boolean and
1962 * start_line1 first line of current hunk (file[0])
1963 * end_line1 last line of current hunk (file[0])
1964 * start_line1 first line of current hunk (file[0])
1965 * end_line1 last line of current hunk (file[0])
1969 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1971 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1972 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1982 pos
= dview
->skip_rows
;
1983 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
1998 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2002 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2003 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2005 pos
= dview
->skip_rows
;
2006 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2010 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2011 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2013 *end_line1
= max (*start_line1
, l0
);
2015 *end_line2
= max (*start_line2
, l1
);
2022 /* --------------------------------------------------------------------------------------------- */
2024 * Remove hunk from file.
2026 * @param dview WDiff widget
2027 * @param merge_file file stream for writing data
2028 * @param from1 first line of hunk
2029 * @param to1 last line of hunk
2030 * @param merge_direction in what direction files should be merged
2034 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2035 action_direction_t merge_direction
)
2041 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2042 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2044 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2047 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2050 fputs (buf
, merge_file
);
2052 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2056 fputs (buf
, merge_file
);
2061 /* --------------------------------------------------------------------------------------------- */
2065 * @param dview WDiff widget
2066 * @param merge_file file stream for writing data
2067 * @param from1 first line of source hunk
2068 * @param from2 first line of destination hunk
2069 * @param to1 last line of source hunk
2070 * @param merge_direction in what direction files should be merged
2074 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2075 action_direction_t merge_direction
)
2082 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2084 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2085 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2089 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2090 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2094 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2097 fputs (buf
, merge_file
);
2100 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2104 fputs (buf
, merge_file
);
2106 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2107 fputs (buf
, merge_file
);
2113 /* --------------------------------------------------------------------------------------------- */
2115 * Replace hunk in file.
2117 * @param dview WDiff widget
2118 * @param merge_file file stream for writing data
2119 * @param from1 first line of source hunk
2120 * @param to1 last line of source hunk
2121 * @param from2 first line of destination hunk
2122 * @param to2 last line of destination hunk
2123 * @param merge_direction in what direction files should be merged
2127 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2128 action_direction_t merge_direction
)
2130 int line1
= 0, line2
= 0;
2135 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2137 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2138 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2142 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2143 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2146 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2149 fputs (buf
, merge_file
);
2151 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2155 fputs (buf
, merge_file
);
2157 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2161 fputs (buf
, merge_file
);
2167 /* --------------------------------------------------------------------------------------------- */
2171 * @param dview WDiff widget
2172 * @param merge_direction in what direction files should be merged
2176 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2178 int from1
, to1
, from2
, to2
;
2181 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2183 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2184 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2186 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2192 vfs_path_t
*merge_file_name_vpath
= NULL
;
2194 if (!dview
->merged
[n_merge
])
2196 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2197 if (!dview
->merged
[n_merge
])
2199 message (D_ERROR
, MSG_ERROR
,
2200 _("Cannot create backup file\n%s%s\n%s"),
2201 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2206 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2207 if (merge_file_fd
== -1)
2209 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2210 unix_error_string (errno
));
2214 merge_file
= fdopen (merge_file_fd
, "w");
2219 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2220 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2222 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2225 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2226 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2228 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2231 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2234 fflush (merge_file
);
2235 fclose (merge_file
);
2236 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2237 mc_unlink (merge_file_name_vpath
);
2238 vfs_path_free (merge_file_name_vpath
);
2242 /* --------------------------------------------------------------------------------------------- */
2243 /* view routines and callbacks ********************************************** */
2246 dview_compute_split (WDiff
* dview
, int i
)
2249 if (dview
->bias
< 2 - dview
->half1
)
2250 dview
->bias
= 2 - dview
->half1
;
2251 if (dview
->bias
> dview
->half2
- 2)
2252 dview
->bias
= dview
->half2
- 2;
2255 /* --------------------------------------------------------------------------------------------- */
2258 dview_compute_areas (WDiff
* dview
)
2260 dview
->height
= LINES
- 2;
2261 dview
->half1
= COLS
/ 2;
2262 dview
->half2
= COLS
- dview
->half1
;
2264 dview_compute_split (dview
, 0);
2267 /* --------------------------------------------------------------------------------------------- */
2270 dview_reread (WDiff
* dview
)
2274 destroy_hdiff (dview
);
2275 if (dview
->a
[DIFF_LEFT
] != NULL
)
2277 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2278 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2280 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2282 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2283 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2286 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2287 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2289 ndiff
= redo_diff (dview
);
2291 dview
->ndiff
= ndiff
;
2294 /* --------------------------------------------------------------------------------------------- */
2298 dview_set_codeset (WDiff
* dview
)
2300 const char *encoding_id
= NULL
;
2304 get_codepage_id (mc_global
.source_codepage
>=
2305 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2306 if (encoding_id
!= NULL
)
2310 conv
= str_crt_conv_from (encoding_id
);
2311 if (conv
!= INVALID_CONV
)
2313 if (dview
->converter
!= str_cnv_from_term
)
2314 str_close_conv (dview
->converter
);
2315 dview
->converter
= conv
;
2317 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
2321 /* --------------------------------------------------------------------------------------------- */
2324 dview_select_encoding (WDiff
* dview
)
2326 if (do_select_codepage ())
2327 dview_set_codeset (dview
);
2328 dview_reread (dview
);
2329 tty_touch_screen ();
2332 #endif /* HAVE_CHARSET */
2334 /* --------------------------------------------------------------------------------------------- */
2337 dview_diff_options (WDiff
* dview
)
2339 const char *quality_str
[] = {
2341 N_("&Fastest (Assume large files)"),
2342 N_("&Minimal (Find a smaller set of change)")
2345 QuickWidget diffopt_widgets
[] = {
2346 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2347 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2349 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2350 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2351 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2352 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2353 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2354 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2355 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2356 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2357 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2358 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2359 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2360 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2361 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2362 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2367 QuickDialog diffopt
= {
2369 N_("Diff Options"), "[Diff Options]",
2370 diffopt_widgets
, NULL
, NULL
, FALSE
2373 if (quick_dialog (&diffopt
) != B_CANCEL
)
2374 dview_reread (dview
);
2377 /* --------------------------------------------------------------------------------------------- */
2380 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2381 const char *label1
, const char *label2
, DSRC dsrc
)
2384 FBUF
*f
[DIFF_COUNT
];
2386 f
[DIFF_LEFT
] = NULL
;
2387 f
[DIFF_RIGHT
] = NULL
;
2389 if (dsrc
== DATA_SRC_TMP
)
2391 f
[DIFF_LEFT
] = f_temp ();
2392 if (f
[DIFF_LEFT
] == NULL
)
2395 f
[DIFF_RIGHT
] = f_temp ();
2396 if (f
[DIFF_RIGHT
] == NULL
)
2398 f_close (f
[DIFF_LEFT
]);
2402 else if (dsrc
== DATA_SRC_ORG
)
2404 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2405 if (f
[DIFF_LEFT
] == NULL
)
2408 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2409 if (f
[DIFF_RIGHT
] == NULL
)
2411 f_close (f
[DIFF_LEFT
]);
2417 dview
->file
[DIFF_LEFT
] = file1
;
2418 dview
->file
[DIFF_RIGHT
] = file2
;
2419 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2420 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2421 dview
->f
[DIFF_LEFT
] = f
[0];
2422 dview
->f
[DIFF_RIGHT
] = f
[1];
2423 dview
->merged
[DIFF_LEFT
] = FALSE
;
2424 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2425 dview
->hdiff
= NULL
;
2427 dview
->converter
= str_cnv_from_term
;
2429 dview_set_codeset (dview
);
2431 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2432 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2434 ndiff
= redo_diff (dview
);
2437 /* goto WIDGET_DESTROY stage: dview_fini() */
2438 f_close (f
[DIFF_LEFT
]);
2439 f_close (f
[DIFF_RIGHT
]);
2443 dview
->ndiff
= ndiff
;
2445 dview
->view_quit
= 0;
2448 dview
->new_frame
= 1;
2449 dview
->skip_rows
= 0;
2450 dview
->skip_cols
= 0;
2451 dview
->display_symbols
= 0;
2452 dview
->display_numbers
= 0;
2454 dview
->tab_size
= 8;
2455 dview
->ord
= DIFF_LEFT
;
2458 dview
->search
.handle
= NULL
;
2459 dview
->search
.last_string
= NULL
;
2460 dview
->search
.last_found_line
= -1;
2461 dview
->search
.last_accessed_num_line
= -1;
2463 dview
->opt
.quality
= 0;
2464 dview
->opt
.strip_trailing_cr
= 0;
2465 dview
->opt
.ignore_tab_expansion
= 0;
2466 dview
->opt
.ignore_space_change
= 0;
2467 dview
->opt
.ignore_all_space
= 0;
2468 dview
->opt
.ignore_case
= 0;
2470 dview_compute_areas (dview
);
2475 /* --------------------------------------------------------------------------------------------- */
2478 dview_fini (WDiff
* dview
)
2480 if (dview
->dsrc
!= DATA_SRC_MEM
)
2482 f_close (dview
->f
[DIFF_RIGHT
]);
2483 f_close (dview
->f
[DIFF_LEFT
]);
2486 if (dview
->converter
!= str_cnv_from_term
)
2487 str_close_conv (dview
->converter
);
2489 destroy_hdiff (dview
);
2490 if (dview
->a
[DIFF_LEFT
] != NULL
)
2492 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2493 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2494 dview
->a
[DIFF_LEFT
] = NULL
;
2496 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2498 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2499 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2500 dview
->a
[DIFF_RIGHT
] = NULL
;
2503 g_free (dview
->label
[DIFF_LEFT
]);
2504 g_free (dview
->label
[DIFF_RIGHT
]);
2507 /* --------------------------------------------------------------------------------------------- */
2510 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2515 FBUF
*f
= dview
->f
[ord
];
2516 int skip
= dview
->skip_cols
;
2517 int display_symbols
= dview
->display_symbols
;
2518 int display_numbers
= dview
->display_numbers
;
2519 int show_cr
= dview
->show_cr
;
2522 int nwidth
= display_numbers
;
2525 xwidth
= display_symbols
+ display_numbers
;
2526 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2527 tab_size
= dview
->tab_size
;
2531 if (xwidth
> width
&& display_symbols
)
2534 display_symbols
= 0;
2536 if (xwidth
> width
&& display_numbers
)
2539 display_numbers
= width
;
2549 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2551 /* abnormal, but avoid buffer overflow */
2555 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2557 int ch
, next_ch
, col
;
2560 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2562 tty_setcolor (NORMAL_COLOR
);
2563 if (display_symbols
)
2565 tty_gotoyx (r
+ j
, c
- 2);
2566 tty_print_char (ch
);
2570 if (display_numbers
)
2572 tty_gotoyx (r
+ j
, c
- xwidth
);
2573 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2574 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2577 tty_setcolor (DFF_ADD_COLOR
);
2579 tty_setcolor (DFF_CHG_COLOR
);
2582 if (i
== (size_t) dview
->search
.last_found_line
)
2583 tty_setcolor (MARKED_SELECTED_COLOR
);
2584 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2589 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2593 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2594 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2595 tty_gotoyx (r
+ j
, c
);
2598 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2605 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2608 if (!g_unichar_isprint (next_ch
))
2612 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2616 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2618 if (mc_global
.utf8_display
)
2623 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2627 else if (dview
->utf8
)
2628 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2630 next_ch
= convert_to_display_c (next_ch
);
2632 tty_print_anychar (next_ch
);
2640 tty_setcolor (DFF_CHH_COLOR
);
2643 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2646 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2649 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2653 if (display_numbers
)
2655 tty_gotoyx (r
+ j
, c
- xwidth
);
2656 memset (buf
, ' ', display_numbers
);
2657 buf
[display_numbers
] = '\0';
2658 tty_print_string (buf
);
2661 tty_setcolor (DFF_DEL_COLOR
);
2663 tty_setcolor (DFF_CHD_COLOR
);
2664 memset (buf
, ' ', width
);
2667 tty_gotoyx (r
+ j
, c
);
2668 /* tty_print_nstring (buf, width); */
2670 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2677 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2680 if (!g_unichar_isprint (next_ch
))
2684 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2688 if (mc_global
.utf8_display
)
2693 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2696 else if (dview
->utf8
)
2697 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2699 next_ch
= convert_to_display_c (next_ch
);
2702 tty_print_anychar (next_ch
);
2707 tty_setcolor (NORMAL_COLOR
);
2709 if (width
< xwidth
- 1)
2711 memset (buf
, ' ', k
);
2713 for (; j
< height
; j
++)
2717 tty_gotoyx (r
+ j
, c
- xwidth
);
2718 /* tty_print_nstring (buf, xwidth - 1); */
2719 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2721 tty_gotoyx (r
+ j
, c
);
2722 /* tty_print_nstring (buf, width); */
2723 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2729 /* --------------------------------------------------------------------------------------------- */
2732 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2736 int linenum
, lineofs
;
2740 tty_setcolor (STATUSBAR_COLOR
);
2743 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2745 filename_width
= width
- 24;
2746 if (filename_width
< 8)
2749 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2750 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2751 vfs_path_free (vpath
);
2752 buf
= str_term_trim (path
, filename_width
);
2753 if (ord
== DIFF_LEFT
)
2754 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2755 buf
, linenum
, lineofs
, dview
->skip_cols
);
2757 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2758 buf
, linenum
, lineofs
, dview
->ndiff
);
2762 /* --------------------------------------------------------------------------------------------- */
2765 dview_redo (WDiff
* dview
)
2767 if (dview
->display_numbers
)
2771 old
= dview
->display_numbers
;
2772 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2773 dview
->new_frame
= (old
!= dview
->display_numbers
);
2775 dview_reread (dview
);
2778 /* --------------------------------------------------------------------------------------------- */
2781 dview_update (WDiff
* dview
)
2783 int height
= dview
->height
;
2788 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2790 if (dview
->skip_rows
> last
)
2791 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2792 if (dview
->skip_rows
< 0)
2793 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2794 if (dview
->skip_cols
< 0)
2795 dview
->skip_cols
= 0;
2800 width1
= dview
->half1
+ dview
->bias
;
2801 width2
= dview
->half2
- dview
->bias
;
2808 if (dview
->new_frame
)
2812 tty_setcolor (NORMAL_COLOR
);
2813 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2815 tty_draw_box (1, 0, height
, width1
, FALSE
);
2817 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2822 if (xwidth
< width1
- 1)
2824 tty_gotoyx (1, xwidth
);
2825 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2826 tty_gotoyx (height
, xwidth
);
2827 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2828 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2830 if (xwidth
< width2
- 1)
2832 tty_gotoyx (1, width1
+ xwidth
);
2833 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2834 tty_gotoyx (height
, width1
+ xwidth
);
2835 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2836 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2839 dview
->new_frame
= 0;
2844 dview_status (dview
, dview
->ord
, width1
, 0);
2845 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2849 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2850 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2854 /* --------------------------------------------------------------------------------------------- */
2857 dview_edit (WDiff
* dview
, diff_place_t ord
)
2861 int linenum
, lineofs
;
2863 if (dview
->dsrc
== DATA_SRC_TMP
)
2865 error_dialog (_("Edit"), _("Edit is disabled"));
2869 h
= ((Widget
*) dview
)->owner
;
2872 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2873 h
->modal
= TRUE
; /* not allow edit file in several editors */
2875 vfs_path_t
*tmp_vpath
;
2877 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2878 do_edit_at_line (tmp_vpath
, use_internal_edit
, linenum
);
2879 vfs_path_free (tmp_vpath
);
2883 dview_update (dview
);
2886 /* --------------------------------------------------------------------------------------------- */
2889 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2892 static const char *title
[2] = {
2893 N_("Goto line (left)"),
2894 N_("Goto line (right)")
2897 static char prev
[256];
2898 /* XXX some statics here, to be remembered between runs */
2903 input
= input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2906 const char *s
= input
;
2908 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2916 for (; i
< dview
->a
[ord
]->len
; i
++)
2918 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2919 if (p
->line
== newline
)
2923 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2924 g_snprintf (prev
, sizeof (prev
), "%d", newline
);
2930 /* --------------------------------------------------------------------------------------------- */
2933 dview_labels (WDiff
* dview
)
2938 h
= dview
->widget
.owner
;
2939 b
= find_buttonbar (h
);
2941 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2942 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2943 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2944 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2945 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2946 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, (Widget
*) dview
);
2947 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2950 /* --------------------------------------------------------------------------------------------- */
2953 dview_event (Gpm_Event
* event
, void *data
)
2955 WDiff
*dview
= (WDiff
*) data
;
2957 if (!mouse_global_in_widget (event
, data
))
2958 return MOU_UNHANDLED
;
2960 /* We are not interested in release events */
2961 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2965 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2967 dview
->skip_rows
-= 2;
2968 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2969 dview_update (dview
);
2971 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2973 dview
->skip_rows
+= 2;
2974 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2975 dview_update (dview
);
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
= 1;
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
, unsigned long command
)
3120 cb_ret_t res
= MSG_HANDLED
;
3124 case CK_ShowSymbols
:
3125 dview
->display_symbols
^= 1;
3126 dview
->new_frame
= 1;
3128 case CK_ShowNumbers
:
3129 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
3130 dview
->new_frame
= 1;
3134 dview
->new_frame
= 1;
3140 dview
->new_frame
= 1;
3146 dview_compute_split (dview
, 1);
3147 dview
->new_frame
= 1;
3154 dview_compute_split (dview
, -1);
3155 dview
->new_frame
= 1;
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
, TRUE
);
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
= 1;
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
)
3284 unsigned long command
;
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_msg_t msg
, int parm
)
3303 WDiff
*dview
= (WDiff
*) w
;
3304 Dlg_head
*h
= dview
->widget
.owner
;
3310 dview_labels (dview
);
3311 dview_load_options (dview
);
3312 dview_update (dview
);
3316 dview
->new_frame
= 1;
3317 dview_update (dview
);
3321 i
= dview_handle_key (dview
, parm
);
3322 if (dview
->view_quit
)
3325 dview_update (dview
);
3328 case WIDGET_COMMAND
:
3329 i
= dview_execute_cmd (dview
, parm
);
3330 if (dview
->view_quit
)
3333 dview_update (dview
);
3336 case WIDGET_DESTROY
:
3337 dview_save_options (dview
);
3342 return default_proc (msg
, parm
);
3346 /* --------------------------------------------------------------------------------------------- */
3349 dview_adjust_size (Dlg_head
* h
)
3354 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3355 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3356 bar
= find_buttonbar (h
);
3357 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3358 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3360 dview_compute_areas (dview
);
3363 /* --------------------------------------------------------------------------------------------- */
3366 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3368 WDiff
*dview
= (WDiff
*) data
;
3373 dview_adjust_size (h
);
3379 return dview_execute_cmd (NULL
, parm
);
3380 /* message from buttonbar */
3381 if (sender
== (Widget
*) find_buttonbar (h
))
3384 return send_message ((Widget
*) data
, WIDGET_COMMAND
, parm
);
3386 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3387 return dview_execute_cmd (dview
, parm
);
3389 return MSG_NOT_HANDLED
;
3392 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3393 h
->state
= DLG_ACTIVE
; /* don't stop the dialog before final decision */
3394 if (dview_ok_to_exit (dview
))
3395 h
->state
= DLG_CLOSED
;
3399 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3403 /* --------------------------------------------------------------------------------------------- */
3406 dview_get_title (const Dlg_head
* h
, size_t len
)
3409 const char *modified
= " (*) ";
3410 const char *notmodified
= " ";
3414 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3415 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3417 title
= g_string_sized_new (len
);
3418 g_string_append (title
, _("Diff:"));
3419 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3420 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3421 g_string_append (title
, " | ");
3422 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3423 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3425 return g_string_free (title
, FALSE
);
3428 /* --------------------------------------------------------------------------------------------- */
3431 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3435 Dlg_head
*dview_dlg
;
3437 /* Create dialog and widgets, put them on the dialog */
3439 create_dlg (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3440 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3442 dview
= g_new0 (WDiff
, 1);
3444 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3445 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3447 widget_want_cursor (dview
->widget
, 0);
3449 add_widget (dview_dlg
, dview
);
3450 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3452 dview_dlg
->get_title
= dview_get_title
;
3454 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3456 /* Please note that if you add another widget,
3457 * you have to modify dview_adjust_size to
3461 run_dlg (dview_dlg
);
3463 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3464 destroy_dlg (dview_dlg
);
3466 return error
== 0 ? 1 : 0;
3469 /*** public functions ****************************************************************************/
3470 /* --------------------------------------------------------------------------------------------- */
3472 #define GET_FILE_AND_STAMP(n) \
3476 real_file##n = file##n; \
3477 if (!vfs_file_is_local (file##n)) \
3479 real_file##n = mc_getlocalcopy (file##n); \
3480 if (real_file##n != NULL) \
3483 if (mc_stat (real_file##n, &st##n) != 0) \
3490 #define UNGET_FILE(n) \
3496 if (use_copy##n > 0) \
3499 mtime = st##n.st_mtime; \
3500 if (mc_stat (real_file##n, &st##n) == 0) \
3501 changed = (mtime != st##n.st_mtime); \
3503 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3504 vfs_path_free (real_file##n); \
3510 dview_diff_cmd (const void *f0
, const void *f1
)
3513 vfs_path_t
*file0
= NULL
;
3514 vfs_path_t
*file1
= NULL
;
3515 gboolean is_dir0
= FALSE
;
3516 gboolean is_dir1
= FALSE
;
3518 switch (mc_global
.mc_run_mode
)
3522 /* run from panels */
3523 const WPanel
*panel0
= (const WPanel
*) f0
;
3524 const WPanel
*panel1
= (const WPanel
*) f1
;
3526 file0
= vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, NULL
);
3527 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3530 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3531 path_trunc (selection (panel0
)->fname
, 30));
3535 file1
= vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, NULL
);
3536 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3539 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3540 path_trunc (selection (panel1
)->fname
, 30));
3546 case MC_RUN_DIFFVIEWER
:
3548 /* run from command line */
3549 const char *p0
= (const char *) f0
;
3550 const char *p1
= (const char *) f1
;
3553 file0
= vfs_path_from_str (p0
);
3554 if (mc_stat (file0
, &st
) == 0)
3556 is_dir0
= S_ISDIR (st
.st_mode
);
3559 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3565 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3566 path_trunc (p0
, 30), unix_error_string (errno
));
3570 file1
= vfs_path_from_str (p1
);
3571 if (mc_stat (file1
, &st
) == 0)
3573 is_dir1
= S_ISDIR (st
.st_mode
);
3576 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3582 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3583 path_trunc (p1
, 30), unix_error_string (errno
));
3590 /* this should not happaned */
3591 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3598 if (file0
!= NULL
&& file1
!= NULL
)
3604 vfs_path_t
*real_file0
;
3605 vfs_path_t
*real_file1
;
3607 GET_FILE_AND_STAMP (0);
3608 GET_FILE_AND_STAMP (1);
3609 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3611 char *real_file0_str
, *real_file1_str
;
3612 char *file0_str
, *file1_str
;
3614 real_file0_str
= vfs_path_to_str (real_file0
);
3615 real_file1_str
= vfs_path_to_str (real_file1
);
3616 file0_str
= vfs_path_to_str (file0
);
3617 file1_str
= vfs_path_to_str (file1
);
3618 rv
= diff_view (real_file0_str
, real_file1_str
, file0_str
, file1_str
);
3619 g_free (real_file0_str
);
3620 g_free (real_file1_str
);
3630 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3633 vfs_path_free (file1
);
3634 vfs_path_free (file0
);
3639 #undef GET_FILE_AND_STAMP
3642 /* --------------------------------------------------------------------------------------------- */