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"
46 #include "lib/strescape.h" /* strutils_glob_escape() */
48 #include "lib/charsets.h"
50 #include "lib/event.h" /* mc_event_raise() */
52 #include "src/filemanager/cmd.h" /* do_edit_at_line(), view_other_cmd() */
53 #include "src/filemanager/panel.h"
54 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
56 #include "src/keybind-defaults.h"
57 #include "src/setup.h"
58 #include "src/history.h"
60 #include "src/selcodepage.h"
66 /*** global variables ****************************************************************************/
68 /*** file scope macro definitions ****************************************************************/
70 #define g_array_foreach(a, TP, cbf) \
72 size_t g_array_foreach_i;\
73 TP *g_array_foreach_var = NULL; \
74 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
76 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
77 (*cbf) (g_array_foreach_var); \
81 #define FILE_READ_BUF 4096
82 #define FILE_FLAG_TEMP (1 << 0)
92 #define HDIFF_ENABLE 1
93 #define HDIFF_MINCTX 5
94 #define HDIFF_DEPTH 10
96 #define FILE_DIRTY(fs) \
104 /*** file scope type declarations ****************************************************************/
110 } action_direction_t
;
112 /*** file scope variables ************************************************************************/
114 /*** file scope functions ************************************************************************/
115 /* --------------------------------------------------------------------------------------------- */
118 TAB_SKIP (int ts
, int pos
)
120 if (ts
> 0 && ts
< 9)
121 return ts
- pos
% ts
;
126 /* --------------------------------------------------------------------------------------------- */
129 rewrite_backup_content (const vfs_path_t
* from_file_name_vpath
, const char *to_file_name
)
134 const char *from_file_name
;
136 from_file_name
= vfs_path_get_by_index (from_file_name_vpath
, -1)->path
;
137 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
140 backup_fd
= fopen (to_file_name
, "w");
141 if (backup_fd
== NULL
)
147 length
= fwrite ((const void *) contents
, length
, 1, backup_fd
);
155 /* buffered I/O ************************************************************* */
158 * Try to open a temporary file.
159 * @note the name is not altered if this function fails
161 * @param[out] name address of a pointer to store the temporary name
162 * @returns file descriptor on success, negative on error
166 open_temp (void **name
)
169 vfs_path_t
*diff_file_name
= NULL
;
171 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
174 message (D_ERROR
, MSG_ERROR
,
175 _("Cannot create temporary diff file\n%s"), unix_error_string (errno
));
178 *name
= vfs_path_to_str (diff_file_name
);
179 vfs_path_free (diff_file_name
);
183 /* --------------------------------------------------------------------------------------------- */
186 * Alocate file structure and associate file descriptor to it.
188 * @param fd file descriptor
189 * @returns file structure
200 fs
= g_try_malloc (sizeof (FBUF
));
204 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
219 /* --------------------------------------------------------------------------------------------- */
222 * Free file structure without closing the file.
224 * @param fs file structure
225 * @returns 0 on success, non-zero on error
233 if (fs
->flags
& FILE_FLAG_TEMP
)
235 rv
= unlink (fs
->data
);
243 /* --------------------------------------------------------------------------------------------- */
246 * Open a binary temporary file in R/W mode.
247 * @note the file will be deleted when closed
249 * @returns file structure
262 fd
= open_temp (&fs
->data
);
270 fs
->flags
= FILE_FLAG_TEMP
;
274 /* --------------------------------------------------------------------------------------------- */
277 * Open a binary file in specified mode.
279 * @param filename file name
280 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
282 * @returns file structure
286 f_open (const char *filename
, int flags
)
295 fd
= open (filename
, flags
);
306 /* --------------------------------------------------------------------------------------------- */
309 * Read a line of bytes from file until newline or EOF.
310 * @note does not stop on null-byte
311 * @note buf will not be null-terminated
313 * @param buf destination buffer
314 * @param size size of buffer
315 * @param fs file structure
317 * @returns number of bytes read
322 f_gets (char *buf
, size_t size
, FBUF
* fs
)
331 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
339 if (j
== size
|| stop
)
343 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
350 /* --------------------------------------------------------------------------------------------- */
354 * @note avoids thrashing read cache when possible
356 * @param fs file structure
358 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
360 * @returns position in file, starting from begginning
365 f_seek (FBUF
* fs
, off_t off
, int whence
)
369 if (fs
->len
&& whence
!= SEEK_END
)
371 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
374 if (whence
== SEEK_CUR
)
377 off
+= rv
- fs
->len
+ fs
->pos
;
379 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
381 fs
->pos
= fs
->len
+ off
- rv
;
387 rv
= lseek (fs
->fd
, off
, whence
);
393 /* --------------------------------------------------------------------------------------------- */
396 * Seek to the beginning of file, thrashing read cache.
398 * @param fs file structure
400 * @returns 0 if success, non-zero on error
408 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
414 /* --------------------------------------------------------------------------------------------- */
417 * Write bytes to file.
418 * @note thrashes read cache
420 * @param fs file structure
421 * @param buf source buffer
422 * @param size size of buffer
424 * @returns number of written bytes, -1 on error
429 f_write (FBUF
* fs
, const char *buf
, size_t size
)
433 rv
= write (fs
->fd
, buf
, size
);
439 /* --------------------------------------------------------------------------------------------- */
442 * Truncate file to the current position.
443 * @note thrashes read cache
445 * @param fs file structure
447 * @returns current file size on success, negative on error
456 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
461 rv
= ftruncate (fs
->fd
, off
);
470 /* --------------------------------------------------------------------------------------------- */
474 * @note if this is temporary file, it is deleted
476 * @param fs file structure
477 * @returns 0 on success, non-zero on error
495 /* --------------------------------------------------------------------------------------------- */
498 * Create pipe stream to process.
500 * @param cmd shell command line
501 * @param flags open mode, either O_RDONLY or O_WRONLY
503 * @returns file structure
507 p_open (const char *cmd
, int flags
)
511 const char *type
= NULL
;
513 if (flags
== O_RDONLY
)
515 else if (flags
== O_WRONLY
)
525 f
= popen (cmd
, type
);
537 /* --------------------------------------------------------------------------------------------- */
542 * @param fs structure
543 * @returns 0 on success, non-zero on error
553 rv
= pclose (fs
->data
);
560 /* --------------------------------------------------------------------------------------------- */
563 * Get one char (byte) from string
565 * @param char * str, gboolean * result
566 * @returns int as character or 0 and result == FALSE if fail
570 dview_get_byte (char *str
, gboolean
* result
)
578 return (unsigned char) *str
;
581 /* --------------------------------------------------------------------------------------------- */
584 * Get utf multibyte char from string
586 * @param char * str, int * char_width, gboolean * result
587 * @returns int as utf character or 0 and result == FALSE if fail
592 dview_get_utf (char *str
, int *char_width
, gboolean
* result
)
596 gchar
*next_ch
= NULL
;
607 res
= g_utf8_get_char_validated (str
, -1);
614 /* Calculate UTF-8 char width */
615 next_ch
= g_utf8_next_char (str
);
617 width
= next_ch
- str
;
625 /* --------------------------------------------------------------------------------------------- */
628 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
632 if (text
== NULL
|| text
[0] == '\0')
635 if (g_utf8_validate (text
, -1, NULL
))
636 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
640 char *tmpbuf
, *buffer
;
642 buffer
= tmpbuf
= g_strdup (text
);
643 while (tmpbuf
[0] != '\0')
645 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
646 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
647 tmpbuf
= g_utf8_next_char (tmpbuf
);
654 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
657 return max (length
, (size_t) result
);
660 /* --------------------------------------------------------------------------------------------- */
662 /* diff parse *************************************************************** */
665 * Read decimal number from string.
667 * @param[in,out] str string to parse
668 * @param[out] n extracted number
669 * @returns 0 if success, otherwise non-zero
673 scan_deci (const char **str
, int *n
)
675 const char *p
= *str
;
679 *n
= strtol (p
, &q
, 10);
680 if (errno
!= 0 || p
== q
)
686 /* --------------------------------------------------------------------------------------------- */
689 * Parse line for diff statement.
691 * @param p string to parse
692 * @param ops list of diff statements
693 * @returns 0 if success, otherwise non-zero
697 scan_line (const char *p
, GArray
* ops
)
706 /* handle the following cases:
708 * NUM[,NUM]cNUM[,NUM]
710 * where NUM is a positive integer
713 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
721 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
733 else if (cmd
!= 'c' && cmd
!= 'd')
736 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
744 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
750 if (cmd
== 'd' && range
!= 0)
758 g_array_append_val (ops
, op
);
762 /* --------------------------------------------------------------------------------------------- */
765 * Parse diff output and extract diff statements.
767 * @param f stream to read from
768 * @param ops list of diff statements to fill
769 * @returns positive number indicating number of hunks, otherwise negative
773 scan_diff (FBUF
* f
, GArray
* ops
)
778 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
780 if (isdigit (buf
[0]))
782 if (buf
[sz
- 1] != '\n')
786 if (scan_line (buf
, ops
) != 0)
792 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
799 /* --------------------------------------------------------------------------------------------- */
802 * Invoke diff and extract diff statements.
804 * @param args extra arguments to be passed to diff
805 * @param extra more arguments to be passed to diff
806 * @param file1 first file to compare
807 * @param file2 second file to compare
808 * @param ops list of diff statements to fill
810 * @returns positive number indicating number of hunks, otherwise negative
814 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
817 static const char *opt
=
818 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
819 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
820 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
821 " --unchanged-group-format=''";
827 char *file1_esc
, *file2_esc
;
829 /* escape potential $ to avoid shell variable substitutions in popen() */
830 file1_esc
= strutils_shell_escape (file1
);
831 file2_esc
= strutils_shell_escape (file2
);
832 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
839 f
= p_open (cmd
, O_RDONLY
);
845 rv
= scan_diff (f
, ops
);
848 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
854 /* --------------------------------------------------------------------------------------------- */
857 * Reparse and display file according to diff statements.
859 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
860 * @param filename file name to display
861 * @param ops list of diff statements
862 * @param printer printf-like function to be used for displaying
863 * @param ctx printer context
865 * @returns 0 if success, otherwise non-zero
869 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
882 f
= f_open (filename
, O_RDONLY
);
898 #define T1 a[ ord^1 ][0]
899 #define T2 a[ ord^1 ][1]
900 for (i
= 0; i
< ops
->len
; i
++)
904 op
= &g_array_index (ops
, DIFFCMD
, i
);
905 n
= op
->F1
- (op
->cmd
!= add_cmd
);
907 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
910 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
912 while (buf
[sz
- 1] != '\n')
914 sz
= f_gets (buf
, sizeof (buf
), f
);
917 printer (ctx
, 0, 0, 0, 1, "\n");
920 printer (ctx
, 0, 0, 0, sz
, buf
);
928 if (op
->cmd
== add_cmd
)
930 n
= op
->T2
- op
->T1
+ 1;
933 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
938 if (op
->cmd
== del_cmd
)
940 n
= op
->F2
- op
->F1
+ 1;
941 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
944 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
946 while (buf
[sz
- 1] != '\n')
948 sz
= f_gets (buf
, sizeof (buf
), f
);
951 printer (ctx
, 0, 0, 0, 1, "\n");
954 printer (ctx
, 0, 0, 0, sz
, buf
);
966 n
= op
->F2
- op
->F1
+ 1;
967 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
970 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
972 while (buf
[sz
- 1] != '\n')
974 sz
= f_gets (buf
, sizeof (buf
), f
);
977 printer (ctx
, 0, 0, 0, 1, "\n");
980 printer (ctx
, 0, 0, 0, sz
, buf
);
989 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
992 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
1002 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1005 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1007 while (buf
[sz
- 1] != '\n')
1009 sz
= f_gets (buf
, sizeof (buf
), f
);
1012 printer (ctx
, 0, 0, 0, 1, "\n");
1015 printer (ctx
, 0, 0, 0, sz
, buf
);
1028 /* --------------------------------------------------------------------------------------------- */
1030 /* horizontal diff ********************************************************** */
1033 * Longest common substring.
1035 * @param s first string
1036 * @param m length of first string
1037 * @param t second string
1038 * @param n length of second string
1039 * @param ret list of offsets for longest common substrings inside each string
1040 * @param min minimum length of common substrings
1042 * @returns 0 if success, nonzero otherwise
1046 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1052 if (m
< min
|| n
< min
)
1054 /* XXX early culling */
1058 Lprev
= g_try_new0 (int, n
+ 1);
1062 Lcurr
= g_try_new0 (int, n
+ 1);
1069 for (i
= 0; i
< m
; i
++)
1076 #ifdef USE_MEMSET_IN_LCS
1077 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1079 for (j
= 0; j
< n
; j
++)
1081 #ifndef USE_MEMSET_IN_LCS
1093 g_array_set_size (ret
, 0);
1095 if (z
== v
&& z
>= min
)
1103 for (k
= 0; k
< ret
->len
; k
++)
1105 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1106 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1115 g_array_append_val (ret
, p2
);
1127 /* --------------------------------------------------------------------------------------------- */
1130 * Scan recursively for common substrings and build ranges.
1132 * @param s first string
1133 * @param t second string
1134 * @param bracket current limits for both of the strings
1135 * @param min minimum length of common substrings
1136 * @param hdiff list of horizontal diff ranges to fill
1137 * @param depth recursion depth
1139 * @returns 0 if success, nonzero otherwise
1143 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1154 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1158 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1159 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1163 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1166 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1167 b
[DIFF_LEFT
].len
= (*data
)[0];
1168 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1169 b
[DIFF_RIGHT
].len
= (*data
)[1];
1170 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1173 for (k
= 0; k
< ret
->len
- 1; k
++)
1175 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1176 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1177 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1178 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1179 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1180 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1181 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1184 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1185 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1186 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1187 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1188 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1189 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1192 g_array_free (ret
, TRUE
);
1197 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1198 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1199 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1200 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1201 g_array_append_val (hdiff
, p
);
1206 /* --------------------------------------------------------------------------------------------- */
1209 * Build list of horizontal diff ranges.
1211 * @param s first string
1212 * @param m length of first string
1213 * @param t second string
1214 * @param n length of second string
1215 * @param min minimum length of common substrings
1216 * @param hdiff list of horizontal diff ranges to fill
1217 * @param depth recursion depth
1219 * @returns 0 if success, nonzero otherwise
1223 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1228 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1229 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1231 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1234 b
[DIFF_LEFT
].off
= i
;
1235 b
[DIFF_LEFT
].len
= m
- i
;
1236 b
[DIFF_RIGHT
].off
= i
;
1237 b
[DIFF_RIGHT
].len
= n
- i
;
1239 /* smartscan (multiple horizontal diff) */
1240 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1243 /* --------------------------------------------------------------------------------------------- */
1245 /* read line **************************************************************** */
1248 * Check if character is inside horizontal diff limits.
1250 * @param k rank of character inside line
1251 * @param hdiff horizontal diff structure
1252 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1254 * @returns TRUE if inside hdiff limits, FALSE otherwise
1258 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1263 for (i
= 0; i
< hdiff
->len
; i
++)
1267 b
= &g_array_index (hdiff
, BRACKET
, i
);
1268 start
= (*b
)[ord
].off
;
1269 end
= start
+ (*b
)[ord
].len
;
1270 if (k
>= start
&& k
< end
)
1276 /* --------------------------------------------------------------------------------------------- */
1279 * Copy 'src' to 'dst' expanding tabs.
1280 * @note The procedure returns when all bytes are consumed from 'src'
1282 * @param dst destination buffer
1283 * @param src source buffer
1284 * @param srcsize size of src buffer
1285 * @param base virtual base of this string, needed to calculate tabs
1286 * @param ts tab size
1288 * @returns new virtual base
1292 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1296 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1303 j
= TAB_SKIP (ts
, i
+ base
);
1313 /* --------------------------------------------------------------------------------------------- */
1316 * Copy 'src' to 'dst' expanding tabs.
1318 * @param dst destination buffer
1319 * @param dstsize size of dst buffer
1320 * @param[in,out] _src source buffer
1321 * @param srcsize size of src buffer
1322 * @param base virtual base of this string, needed to calculate tabs
1323 * @param ts tab size
1325 * @returns new virtual base
1327 * @note The procedure returns when all bytes are consumed from 'src'
1328 * or 'dstsize' bytes are written to 'dst'
1329 * @note Upon return, 'src' points to the first unwritten character in source
1333 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1336 const char *src
= *_src
;
1338 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1345 j
= TAB_SKIP (ts
, i
+ base
);
1346 if (j
> dstsize
- i
)
1358 /* --------------------------------------------------------------------------------------------- */
1361 * Read line from memory, converting tabs to spaces and padding with spaces.
1363 * @param src buffer to read from
1364 * @param srcsize size of src buffer
1365 * @param dst buffer to read to
1366 * @param dstsize size of dst buffer, excluding trailing null
1367 * @param skip number of characters to skip
1368 * @param ts tab size
1369 * @param show_cr show trailing carriage return as ^M
1371 * @returns negative on error, otherwise number of bytes except padding
1375 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1385 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1391 j
= TAB_SKIP (ts
, i
+ base
);
1397 else if (dstsize
!= 0)
1404 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1406 if (skip
== 0 && show_cr
)
1429 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1432 if (!g_unichar_isprint (utf_ch
))
1443 while (dstsize
!= 0)
1452 /* --------------------------------------------------------------------------------------------- */
1455 * Read line from memory and build attribute array.
1457 * @param src buffer to read from
1458 * @param srcsize size of src buffer
1459 * @param dst buffer to read to
1460 * @param dstsize size of dst buffer, excluding trailing null
1461 * @param skip number of characters to skip
1462 * @param ts tab size
1463 * @param show_cr show trailing carriage return as ^M
1464 * @param hdiff horizontal diff structure
1465 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1466 * @param att buffer of attributes
1468 * @returns negative on error, otherwise number of bytes except padding
1472 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1473 GArray
* hdiff
, diff_place_t ord
, char *att
)
1483 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1489 j
= TAB_SKIP (ts
, i
+ base
);
1495 else if (dstsize
!= 0)
1498 *att
++ = is_inside (k
, hdiff
, ord
);
1503 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1505 if (skip
== 0 && show_cr
)
1510 *att
++ = is_inside (k
, hdiff
, ord
);
1512 *att
++ = is_inside (k
, hdiff
, ord
);
1518 *att
++ = is_inside (k
, hdiff
, ord
);
1531 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1534 if (!g_unichar_isprint (utf_ch
))
1540 *att
++ = is_inside (k
, hdiff
, ord
);
1546 while (dstsize
!= 0)
1556 /* --------------------------------------------------------------------------------------------- */
1559 * Read line from file, converting tabs to spaces and padding with spaces.
1561 * @param f file stream to read from
1562 * @param off offset of line inside file
1563 * @param dst buffer to read to
1564 * @param dstsize size of dst buffer, excluding trailing null
1565 * @param skip number of characters to skip
1566 * @param ts tab size
1567 * @param show_cr show trailing carriage return as ^M
1569 * @returns negative on error, otherwise number of bytes except padding
1573 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1576 int old_base
= base
;
1577 size_t amount
= dstsize
;
1578 size_t useful
, offset
;
1582 const char *q
= NULL
;
1583 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1584 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1586 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1588 /* abnormal, but avoid buffer overflow */
1589 memset (dst
, ' ', dstsize
);
1590 dst
[dstsize
] = '\0';
1594 f_seek (f
, off
, SEEK_SET
);
1599 sz
= f_gets (tmp
, amount
, f
);
1603 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1604 if (cvt
[base
- old_base
- 1] == '\n')
1606 q
= &cvt
[base
- old_base
- 1];
1607 base
= old_base
+ q
- cvt
+ 1;
1614 memset (dst
, ' ', dstsize
);
1615 dst
[dstsize
] = '\0';
1619 useful
= base
- skip
;
1620 offset
= skip
- old_base
;
1622 if (useful
<= dstsize
)
1625 memmove (dst
, cvt
+ offset
, useful
);
1629 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1632 const char *ptr
= tmp
;
1634 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1643 memmove (dst
, cvt
+ offset
, dstsize
);
1645 lastch
= cvt
[offset
+ dstsize
];
1649 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1651 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1655 if (i
+ 1 < dstsize
)
1669 for (; i
< dstsize
; i
++)
1675 /* --------------------------------------------------------------------------------------------- */
1676 /* diff printers et al ****************************************************** */
1679 cc_free_elt (void *elt
)
1687 /* --------------------------------------------------------------------------------------------- */
1690 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1692 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1693 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1703 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1705 if (sz
!= 0 && str
[sz
- 1] == '\n')
1708 p
.p
= g_strndup (str
, sz
);
1711 g_array_append_val (a
, p
);
1713 else if (dsrc
== DATA_SRC_MEM
)
1717 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1718 if (sz
!= 0 && str
[sz
- 1] == '\n')
1725 new_size
= p
->u
.len
+ sz
;
1726 q
= g_realloc (p
->p
, new_size
);
1727 memcpy (q
+ p
->u
.len
, str
, sz
);
1732 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1734 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1735 f_write (f
, str
, sz
);
1740 /* --------------------------------------------------------------------------------------------- */
1743 redo_diff (WDiff
* dview
)
1745 FBUF
*const *f
= dview
->f
;
1753 if (dview
->opt
.quality
== 2)
1754 strcat (extra
, " -d");
1755 if (dview
->opt
.quality
== 1)
1756 strcat (extra
, " --speed-large-files");
1757 if (dview
->opt
.strip_trailing_cr
)
1758 strcat (extra
, " --strip-trailing-cr");
1759 if (dview
->opt
.ignore_tab_expansion
)
1760 strcat (extra
, " -E");
1761 if (dview
->opt
.ignore_space_change
)
1762 strcat (extra
, " -b");
1763 if (dview
->opt
.ignore_all_space
)
1764 strcat (extra
, " -w");
1765 if (dview
->opt
.ignore_case
)
1766 strcat (extra
, " -i");
1768 if (dview
->dsrc
!= DATA_SRC_MEM
)
1770 f_reset (f
[DIFF_LEFT
]);
1771 f_reset (f
[DIFF_RIGHT
]);
1774 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1775 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1779 g_array_free (ops
, TRUE
);
1783 ctx
.dsrc
= dview
->dsrc
;
1786 ctx
.a
= dview
->a
[DIFF_LEFT
];
1787 ctx
.f
= f
[DIFF_LEFT
];
1788 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1790 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1791 ctx
.f
= f
[DIFF_RIGHT
];
1792 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1795 g_array_free (ops
, TRUE
);
1797 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1800 if (dview
->dsrc
== DATA_SRC_TMP
)
1802 f_trunc (f
[DIFF_LEFT
]);
1803 f_trunc (f
[DIFF_RIGHT
]);
1806 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1808 dview
->hdiff
= g_ptr_array_new ();
1809 if (dview
->hdiff
!= NULL
)
1815 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1819 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1820 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1821 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1823 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1829 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1833 g_array_free (h
, TRUE
);
1838 g_ptr_array_add (dview
->hdiff
, h
);
1845 /* --------------------------------------------------------------------------------------------- */
1848 destroy_hdiff (WDiff
* dview
)
1850 if (dview
->hdiff
!= NULL
)
1855 len
= dview
->a
[DIFF_LEFT
]->len
;
1857 for (i
= 0; i
< len
; i
++)
1861 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1863 g_array_free (h
, TRUE
);
1865 g_ptr_array_free (dview
->hdiff
, TRUE
);
1866 dview
->hdiff
= NULL
;
1869 mc_search_free (dview
->search
.handle
);
1870 dview
->search
.handle
= NULL
;
1871 g_free (dview
->search
.last_string
);
1872 dview
->search
.last_string
= NULL
;
1875 /* --------------------------------------------------------------------------------------------- */
1876 /* stuff ******************************************************************** */
1879 get_digits (unsigned int n
)
1888 /* --------------------------------------------------------------------------------------------- */
1891 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1903 p
= &g_array_index (a
, DIFFLN
, pos
);
1909 for (n
= pos
; n
> 0; n
--)
1915 *lineofs
= pos
- n
+ 1;
1923 /* --------------------------------------------------------------------------------------------- */
1926 calc_nwidth (const GArray
** const a
)
1931 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1932 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1935 return get_digits (l1
);
1938 /* --------------------------------------------------------------------------------------------- */
1941 find_prev_hunk (const GArray
* a
, int pos
)
1944 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1946 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1948 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1950 if (pos
> 0 && (size_t) pos
< a
->len
)
1953 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1955 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1962 /* --------------------------------------------------------------------------------------------- */
1965 find_next_hunk (const GArray
* a
, size_t pos
)
1967 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1969 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1974 /* --------------------------------------------------------------------------------------------- */
1976 * Find start and end lines of the current hunk.
1978 * @param dview WDiff widget
1979 * @returns boolean and
1980 * start_line1 first line of current hunk (file[0])
1981 * end_line1 last line of current hunk (file[0])
1982 * start_line1 first line of current hunk (file[0])
1983 * end_line1 last line of current hunk (file[0])
1987 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1989 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1990 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
2000 pos
= dview
->skip_rows
;
2001 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2016 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2020 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2021 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2023 pos
= dview
->skip_rows
;
2024 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2028 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2029 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2031 *end_line1
= max (*start_line1
, l0
);
2033 *end_line2
= max (*start_line2
, l1
);
2040 /* --------------------------------------------------------------------------------------------- */
2042 * Remove hunk from file.
2044 * @param dview WDiff widget
2045 * @param merge_file file stream for writing data
2046 * @param from1 first line of hunk
2047 * @param to1 last line of hunk
2048 * @param merge_direction in what direction files should be merged
2052 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2053 action_direction_t merge_direction
)
2059 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2060 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2062 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2065 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2068 fputs (buf
, merge_file
);
2070 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2074 fputs (buf
, merge_file
);
2079 /* --------------------------------------------------------------------------------------------- */
2083 * @param dview WDiff widget
2084 * @param merge_file file stream for writing data
2085 * @param from1 first line of source hunk
2086 * @param from2 first line of destination hunk
2087 * @param to1 last line of source hunk
2088 * @param merge_direction in what direction files should be merged
2092 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2093 action_direction_t merge_direction
)
2100 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2102 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2103 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2107 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2108 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2112 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2115 fputs (buf
, merge_file
);
2118 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2122 fputs (buf
, merge_file
);
2124 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2125 fputs (buf
, merge_file
);
2131 /* --------------------------------------------------------------------------------------------- */
2133 * Replace hunk in file.
2135 * @param dview WDiff widget
2136 * @param merge_file file stream for writing data
2137 * @param from1 first line of source hunk
2138 * @param to1 last line of source hunk
2139 * @param from2 first line of destination hunk
2140 * @param to2 last line of destination hunk
2141 * @param merge_direction in what direction files should be merged
2145 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2146 action_direction_t merge_direction
)
2148 int line1
= 0, line2
= 0;
2153 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2155 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2156 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2160 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2161 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2164 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2167 fputs (buf
, merge_file
);
2169 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2173 fputs (buf
, merge_file
);
2175 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2179 fputs (buf
, merge_file
);
2185 /* --------------------------------------------------------------------------------------------- */
2189 * @param dview WDiff widget
2190 * @param merge_direction in what direction files should be merged
2194 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2196 int from1
, to1
, from2
, to2
;
2199 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2201 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2202 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2204 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2210 vfs_path_t
*merge_file_name_vpath
= NULL
;
2212 if (!dview
->merged
[n_merge
])
2214 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2215 if (!dview
->merged
[n_merge
])
2217 message (D_ERROR
, MSG_ERROR
,
2218 _("Cannot create backup file\n%s%s\n%s"),
2219 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2224 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2225 if (merge_file_fd
== -1)
2227 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2228 unix_error_string (errno
));
2232 merge_file
= fdopen (merge_file_fd
, "w");
2237 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2238 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2240 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2243 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2244 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2246 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2249 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2252 fflush (merge_file
);
2253 fclose (merge_file
);
2254 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 QuickWidget diffopt_widgets
[] = {
2364 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2365 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2367 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2368 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2369 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2370 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2371 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2372 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2373 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2374 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2375 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2376 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2377 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2378 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2379 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2380 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2385 QuickDialog diffopt
= {
2387 N_("Diff Options"), "[Diff Options]",
2388 diffopt_widgets
, NULL
, NULL
, FALSE
2391 if (quick_dialog (&diffopt
) != B_CANCEL
)
2392 dview_reread (dview
);
2395 /* --------------------------------------------------------------------------------------------- */
2398 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2399 const char *label1
, const char *label2
, DSRC dsrc
)
2402 FBUF
*f
[DIFF_COUNT
];
2404 f
[DIFF_LEFT
] = NULL
;
2405 f
[DIFF_RIGHT
] = NULL
;
2407 if (dsrc
== DATA_SRC_TMP
)
2409 f
[DIFF_LEFT
] = f_temp ();
2410 if (f
[DIFF_LEFT
] == NULL
)
2413 f
[DIFF_RIGHT
] = f_temp ();
2414 if (f
[DIFF_RIGHT
] == NULL
)
2416 f_close (f
[DIFF_LEFT
]);
2420 else if (dsrc
== DATA_SRC_ORG
)
2422 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2423 if (f
[DIFF_LEFT
] == NULL
)
2426 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2427 if (f
[DIFF_RIGHT
] == NULL
)
2429 f_close (f
[DIFF_LEFT
]);
2435 dview
->file
[DIFF_LEFT
] = file1
;
2436 dview
->file
[DIFF_RIGHT
] = file2
;
2437 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2438 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2439 dview
->f
[DIFF_LEFT
] = f
[0];
2440 dview
->f
[DIFF_RIGHT
] = f
[1];
2441 dview
->merged
[DIFF_LEFT
] = FALSE
;
2442 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2443 dview
->hdiff
= NULL
;
2445 dview
->converter
= str_cnv_from_term
;
2447 dview_set_codeset (dview
);
2449 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2450 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2452 ndiff
= redo_diff (dview
);
2455 /* goto WIDGET_DESTROY stage: dview_fini() */
2456 f_close (f
[DIFF_LEFT
]);
2457 f_close (f
[DIFF_RIGHT
]);
2461 dview
->ndiff
= ndiff
;
2463 dview
->view_quit
= 0;
2466 dview
->new_frame
= 1;
2467 dview
->skip_rows
= 0;
2468 dview
->skip_cols
= 0;
2469 dview
->display_symbols
= 0;
2470 dview
->display_numbers
= 0;
2472 dview
->tab_size
= 8;
2473 dview
->ord
= DIFF_LEFT
;
2476 dview
->search
.handle
= NULL
;
2477 dview
->search
.last_string
= NULL
;
2478 dview
->search
.last_found_line
= -1;
2479 dview
->search
.last_accessed_num_line
= -1;
2481 dview
->opt
.quality
= 0;
2482 dview
->opt
.strip_trailing_cr
= 0;
2483 dview
->opt
.ignore_tab_expansion
= 0;
2484 dview
->opt
.ignore_space_change
= 0;
2485 dview
->opt
.ignore_all_space
= 0;
2486 dview
->opt
.ignore_case
= 0;
2488 dview_compute_areas (dview
);
2493 /* --------------------------------------------------------------------------------------------- */
2496 dview_fini (WDiff
* dview
)
2498 if (dview
->dsrc
!= DATA_SRC_MEM
)
2500 f_close (dview
->f
[DIFF_RIGHT
]);
2501 f_close (dview
->f
[DIFF_LEFT
]);
2504 if (dview
->converter
!= str_cnv_from_term
)
2505 str_close_conv (dview
->converter
);
2507 destroy_hdiff (dview
);
2508 if (dview
->a
[DIFF_LEFT
] != NULL
)
2510 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2511 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2512 dview
->a
[DIFF_LEFT
] = NULL
;
2514 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2516 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2517 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2518 dview
->a
[DIFF_RIGHT
] = NULL
;
2521 g_free (dview
->label
[DIFF_LEFT
]);
2522 g_free (dview
->label
[DIFF_RIGHT
]);
2525 /* --------------------------------------------------------------------------------------------- */
2528 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2533 FBUF
*f
= dview
->f
[ord
];
2534 int skip
= dview
->skip_cols
;
2535 int display_symbols
= dview
->display_symbols
;
2536 int display_numbers
= dview
->display_numbers
;
2537 int show_cr
= dview
->show_cr
;
2540 int nwidth
= display_numbers
;
2543 xwidth
= display_symbols
+ display_numbers
;
2544 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2545 tab_size
= dview
->tab_size
;
2549 if (xwidth
> width
&& display_symbols
)
2552 display_symbols
= 0;
2554 if (xwidth
> width
&& display_numbers
)
2557 display_numbers
= width
;
2567 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2569 /* abnormal, but avoid buffer overflow */
2573 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2575 int ch
, next_ch
, col
;
2578 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2580 tty_setcolor (NORMAL_COLOR
);
2581 if (display_symbols
)
2583 tty_gotoyx (r
+ j
, c
- 2);
2584 tty_print_char (ch
);
2588 if (display_numbers
)
2590 tty_gotoyx (r
+ j
, c
- xwidth
);
2591 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2592 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2595 tty_setcolor (DFF_ADD_COLOR
);
2597 tty_setcolor (DFF_CHG_COLOR
);
2600 if (i
== (size_t) dview
->search
.last_found_line
)
2601 tty_setcolor (MARKED_SELECTED_COLOR
);
2602 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2607 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
++)
2623 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2626 if (!g_unichar_isprint (next_ch
))
2630 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2634 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2636 if (mc_global
.utf8_display
)
2641 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2645 else if (dview
->utf8
)
2646 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2648 next_ch
= convert_to_display_c (next_ch
);
2650 tty_print_anychar (next_ch
);
2658 tty_setcolor (DFF_CHH_COLOR
);
2661 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2664 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2667 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2671 if (display_numbers
)
2673 tty_gotoyx (r
+ j
, c
- xwidth
);
2674 memset (buf
, ' ', display_numbers
);
2675 buf
[display_numbers
] = '\0';
2676 tty_print_string (buf
);
2679 tty_setcolor (DFF_DEL_COLOR
);
2681 tty_setcolor (DFF_CHD_COLOR
);
2682 memset (buf
, ' ', width
);
2685 tty_gotoyx (r
+ j
, c
);
2686 /* tty_print_nstring (buf, width); */
2688 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2695 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2698 if (!g_unichar_isprint (next_ch
))
2702 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2706 if (mc_global
.utf8_display
)
2711 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2714 else if (dview
->utf8
)
2715 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2717 next_ch
= convert_to_display_c (next_ch
);
2720 tty_print_anychar (next_ch
);
2725 tty_setcolor (NORMAL_COLOR
);
2727 if (width
< xwidth
- 1)
2729 memset (buf
, ' ', k
);
2731 for (; j
< height
; j
++)
2735 tty_gotoyx (r
+ j
, c
- xwidth
);
2736 /* tty_print_nstring (buf, xwidth - 1); */
2737 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2739 tty_gotoyx (r
+ j
, c
);
2740 /* tty_print_nstring (buf, width); */
2741 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2747 /* --------------------------------------------------------------------------------------------- */
2750 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2754 int linenum
, lineofs
;
2758 tty_setcolor (STATUSBAR_COLOR
);
2761 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2763 filename_width
= width
- 24;
2764 if (filename_width
< 8)
2767 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2768 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2769 vfs_path_free (vpath
);
2770 buf
= str_term_trim (path
, filename_width
);
2771 if (ord
== DIFF_LEFT
)
2772 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2773 buf
, linenum
, lineofs
, dview
->skip_cols
);
2775 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2776 buf
, linenum
, lineofs
, dview
->ndiff
);
2780 /* --------------------------------------------------------------------------------------------- */
2783 dview_redo (WDiff
* dview
)
2785 if (dview
->display_numbers
)
2789 old
= dview
->display_numbers
;
2790 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2791 dview
->new_frame
= (old
!= dview
->display_numbers
);
2793 dview_reread (dview
);
2796 /* --------------------------------------------------------------------------------------------- */
2799 dview_update (WDiff
* dview
)
2801 int height
= dview
->height
;
2806 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2808 if (dview
->skip_rows
> last
)
2809 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2810 if (dview
->skip_rows
< 0)
2811 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2812 if (dview
->skip_cols
< 0)
2813 dview
->skip_cols
= 0;
2818 width1
= dview
->half1
+ dview
->bias
;
2819 width2
= dview
->half2
- dview
->bias
;
2826 if (dview
->new_frame
)
2830 tty_setcolor (NORMAL_COLOR
);
2831 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2833 tty_draw_box (1, 0, height
, width1
, FALSE
);
2835 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2840 if (xwidth
< width1
- 1)
2842 tty_gotoyx (1, xwidth
);
2843 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2844 tty_gotoyx (height
, xwidth
);
2845 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2846 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2848 if (xwidth
< width2
- 1)
2850 tty_gotoyx (1, width1
+ xwidth
);
2851 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2852 tty_gotoyx (height
, width1
+ xwidth
);
2853 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2854 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2857 dview
->new_frame
= 0;
2862 dview_status (dview
, dview
->ord
, width1
, 0);
2863 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2867 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2868 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2872 /* --------------------------------------------------------------------------------------------- */
2875 dview_edit (WDiff
* dview
, diff_place_t ord
)
2879 int linenum
, lineofs
;
2881 if (dview
->dsrc
== DATA_SRC_TMP
)
2883 error_dialog (_("Edit"), _("Edit is disabled"));
2887 h
= ((Widget
*) dview
)->owner
;
2890 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2891 h
->modal
= TRUE
; /* not allow edit file in several editors */
2893 vfs_path_t
*tmp_vpath
;
2895 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2896 do_edit_at_line (tmp_vpath
, use_internal_edit
, linenum
);
2897 vfs_path_free (tmp_vpath
);
2901 dview_update (dview
);
2904 /* --------------------------------------------------------------------------------------------- */
2907 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2910 static const char *title
[2] = {
2911 N_("Goto line (left)"),
2912 N_("Goto line (right)")
2915 static char prev
[256];
2916 /* XXX some statics here, to be remembered between runs */
2921 input
= input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2924 const char *s
= input
;
2926 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2934 for (; i
< dview
->a
[ord
]->len
; i
++)
2936 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2937 if (p
->line
== newline
)
2941 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2942 g_snprintf (prev
, sizeof (prev
), "%d", newline
);
2948 /* --------------------------------------------------------------------------------------------- */
2951 dview_labels (WDiff
* dview
)
2956 h
= dview
->widget
.owner
;
2957 b
= find_buttonbar (h
);
2959 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2960 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2961 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2962 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2963 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2964 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, (Widget
*) dview
);
2965 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2968 /* --------------------------------------------------------------------------------------------- */
2971 dview_event (Gpm_Event
* event
, void *data
)
2973 WDiff
*dview
= (WDiff
*) data
;
2975 if (!mouse_global_in_widget (event
, data
))
2976 return MOU_UNHANDLED
;
2978 /* We are not interested in release events */
2979 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2983 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2985 dview
->skip_rows
-= 2;
2986 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2987 dview_update (dview
);
2989 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2991 dview
->skip_rows
+= 2;
2992 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2993 dview_update (dview
);
2999 /* --------------------------------------------------------------------------------------------- */
3002 dview_save (WDiff
* dview
)
3004 gboolean res
= TRUE
;
3006 if (dview
->merged
[DIFF_LEFT
])
3008 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3009 dview
->merged
[DIFF_LEFT
] = !res
;
3011 if (dview
->merged
[DIFF_RIGHT
])
3013 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3014 dview
->merged
[DIFF_RIGHT
] = !res
;
3019 /* --------------------------------------------------------------------------------------------- */
3022 dview_do_save (WDiff
* dview
)
3024 (void) dview_save (dview
);
3027 /* --------------------------------------------------------------------------------------------- */
3030 dview_save_options (WDiff
* dview
)
3032 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
3033 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
3034 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
3035 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
3036 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
3038 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3040 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
3041 dview
->opt
.strip_trailing_cr
);
3042 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
3043 dview
->opt
.ignore_all_space
);
3044 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
3045 dview
->opt
.ignore_space_change
);
3046 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
3047 dview
->opt
.ignore_tab_expansion
);
3048 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
3051 /* --------------------------------------------------------------------------------------------- */
3054 dview_load_options (WDiff
* dview
)
3056 gboolean show_numbers
, show_symbols
;
3059 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
3061 dview
->display_symbols
= 1;
3062 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
3064 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
3065 tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
3066 if (tab_size
> 0 && tab_size
< 9)
3067 dview
->tab_size
= tab_size
;
3069 dview
->tab_size
= 8;
3071 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
3073 dview
->opt
.strip_trailing_cr
=
3074 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3075 dview
->opt
.ignore_all_space
=
3076 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3077 dview
->opt
.ignore_space_change
=
3078 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3079 dview
->opt
.ignore_tab_expansion
=
3080 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3081 dview
->opt
.ignore_case
=
3082 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
3084 dview
->new_frame
= 1;
3087 /* --------------------------------------------------------------------------------------------- */
3090 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3094 dview_ok_to_exit (WDiff
* dview
)
3096 gboolean res
= TRUE
;
3099 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3102 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3103 _("File(s) was modified. Save with exit?") :
3104 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3105 D_NORMAL
, 2, _("&Yes"), _("&No"));
3108 if (mc_global
.midnight_shutdown
|| (act
== -1))
3117 (void) dview_save (dview
);
3121 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3122 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3123 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3124 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3133 /* --------------------------------------------------------------------------------------------- */
3136 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
3138 cb_ret_t res
= MSG_HANDLED
;
3142 case CK_ShowSymbols
:
3143 dview
->display_symbols
^= 1;
3144 dview
->new_frame
= 1;
3146 case CK_ShowNumbers
:
3147 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
3148 dview
->new_frame
= 1;
3152 dview
->new_frame
= 1;
3158 dview
->new_frame
= 1;
3164 dview_compute_split (dview
, 1);
3165 dview
->new_frame
= 1;
3172 dview_compute_split (dview
, -1);
3173 dview
->new_frame
= 1;
3177 dview
->tab_size
= 2;
3180 dview
->tab_size
= 3;
3183 dview
->tab_size
= 4;
3186 dview
->tab_size
= 8;
3195 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3196 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3199 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3200 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3203 dview_goto_cmd (dview
, TRUE
);
3206 dview_edit (dview
, dview
->ord
);
3209 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3213 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3217 dview_edit (dview
, dview
->ord
^ 1);
3220 dview_search_cmd (dview
);
3222 case CK_SearchContinue
:
3223 dview_continue_search_cmd (dview
);
3226 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3229 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3232 if (dview
->skip_rows
> 0)
3235 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3240 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3243 if (dview
->height
> 2)
3245 dview
->skip_rows
+= dview
->height
- 2;
3246 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3250 if (dview
->height
> 2)
3252 dview
->skip_rows
-= dview
->height
- 2;
3253 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3263 dview
->skip_cols
-= 8;
3266 dview
->skip_cols
+= 8;
3269 dview
->skip_cols
= 0;
3275 dview
->view_quit
= 1;
3278 dview_do_save (dview
);
3281 dview_diff_options (dview
);
3284 case CK_SelectCodepage
:
3285 dview_select_encoding (dview
);
3289 /* don't close diffviewer due to SIGINT */
3292 res
= MSG_NOT_HANDLED
;
3297 /* --------------------------------------------------------------------------------------------- */
3300 dview_handle_key (WDiff
* dview
, int key
)
3302 unsigned long command
;
3305 key
= convert_from_input_c (key
);
3308 command
= keybind_lookup_keymap_command (diff_map
, key
);
3309 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3313 return MSG_NOT_HANDLED
;
3316 /* --------------------------------------------------------------------------------------------- */
3319 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3321 WDiff
*dview
= (WDiff
*) w
;
3322 Dlg_head
*h
= dview
->widget
.owner
;
3328 dview_labels (dview
);
3329 dview_load_options (dview
);
3330 dview_update (dview
);
3334 dview
->new_frame
= 1;
3335 dview_update (dview
);
3339 i
= dview_handle_key (dview
, parm
);
3340 if (dview
->view_quit
)
3343 dview_update (dview
);
3346 case WIDGET_COMMAND
:
3347 i
= dview_execute_cmd (dview
, parm
);
3348 if (dview
->view_quit
)
3351 dview_update (dview
);
3354 case WIDGET_DESTROY
:
3355 dview_save_options (dview
);
3360 return default_proc (msg
, parm
);
3364 /* --------------------------------------------------------------------------------------------- */
3367 dview_adjust_size (Dlg_head
* h
)
3372 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3373 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3374 bar
= find_buttonbar (h
);
3375 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3376 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3378 dview_compute_areas (dview
);
3381 /* --------------------------------------------------------------------------------------------- */
3384 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3386 WDiff
*dview
= (WDiff
*) data
;
3391 dview_adjust_size (h
);
3397 return dview_execute_cmd (NULL
, parm
);
3398 /* message from buttonbar */
3399 if (sender
== (Widget
*) find_buttonbar (h
))
3402 return send_message ((Widget
*) data
, WIDGET_COMMAND
, parm
);
3404 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3405 return dview_execute_cmd (dview
, parm
);
3407 return MSG_NOT_HANDLED
;
3410 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3411 h
->state
= DLG_ACTIVE
; /* don't stop the dialog before final decision */
3412 if (dview_ok_to_exit (dview
))
3413 h
->state
= DLG_CLOSED
;
3417 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3421 /* --------------------------------------------------------------------------------------------- */
3424 dview_get_title (const Dlg_head
* h
, size_t len
)
3427 const char *modified
= " (*) ";
3428 const char *notmodified
= " ";
3432 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3433 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3435 title
= g_string_sized_new (len
);
3436 g_string_append (title
, _("Diff:"));
3437 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3438 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3439 g_string_append (title
, " | ");
3440 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3441 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3443 return g_string_free (title
, FALSE
);
3446 /* --------------------------------------------------------------------------------------------- */
3449 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3453 Dlg_head
*dview_dlg
;
3455 /* Create dialog and widgets, put them on the dialog */
3457 create_dlg (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3458 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3460 dview
= g_new0 (WDiff
, 1);
3462 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3463 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3465 widget_want_cursor (dview
->widget
, 0);
3467 add_widget (dview_dlg
, dview
);
3468 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3470 dview_dlg
->get_title
= dview_get_title
;
3472 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3474 /* Please note that if you add another widget,
3475 * you have to modify dview_adjust_size to
3479 run_dlg (dview_dlg
);
3481 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3482 destroy_dlg (dview_dlg
);
3484 return error
== 0 ? 1 : 0;
3487 /*** public functions ****************************************************************************/
3488 /* --------------------------------------------------------------------------------------------- */
3490 #define GET_FILE_AND_STAMP(n) \
3494 real_file##n = file##n; \
3495 if (!vfs_file_is_local (file##n)) \
3497 real_file##n = mc_getlocalcopy (file##n); \
3498 if (real_file##n != NULL) \
3501 if (mc_stat (real_file##n, &st##n) != 0) \
3508 #define UNGET_FILE(n) \
3514 if (use_copy##n > 0) \
3517 mtime = st##n.st_mtime; \
3518 if (mc_stat (real_file##n, &st##n) == 0) \
3519 changed = (mtime != st##n.st_mtime); \
3521 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3522 vfs_path_free (real_file##n); \
3528 dview_diff_cmd (const void *f0
, const void *f1
)
3531 vfs_path_t
*file0
= NULL
;
3532 vfs_path_t
*file1
= NULL
;
3533 gboolean is_dir0
= FALSE
;
3534 gboolean is_dir1
= FALSE
;
3536 switch (mc_global
.mc_run_mode
)
3540 /* run from panels */
3541 const WPanel
*panel0
= (const WPanel
*) f0
;
3542 const WPanel
*panel1
= (const WPanel
*) f1
;
3544 file0
= vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, NULL
);
3545 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3548 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3549 path_trunc (selection (panel0
)->fname
, 30));
3553 file1
= vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, NULL
);
3554 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3557 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3558 path_trunc (selection (panel1
)->fname
, 30));
3564 case MC_RUN_DIFFVIEWER
:
3566 /* run from command line */
3567 const char *p0
= (const char *) f0
;
3568 const char *p1
= (const char *) f1
;
3571 file0
= vfs_path_from_str (p0
);
3572 if (mc_stat (file0
, &st
) == 0)
3574 is_dir0
= S_ISDIR (st
.st_mode
);
3577 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3583 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3584 path_trunc (p0
, 30), unix_error_string (errno
));
3588 file1
= vfs_path_from_str (p1
);
3589 if (mc_stat (file1
, &st
) == 0)
3591 is_dir1
= S_ISDIR (st
.st_mode
);
3594 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3600 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3601 path_trunc (p1
, 30), unix_error_string (errno
));
3608 /* this should not happaned */
3609 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3616 if (file0
!= NULL
&& file1
!= NULL
)
3622 vfs_path_t
*real_file0
;
3623 vfs_path_t
*real_file1
;
3625 GET_FILE_AND_STAMP (0);
3626 GET_FILE_AND_STAMP (1);
3627 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3629 char *real_file0_str
, *real_file1_str
;
3630 char *file0_str
, *file1_str
;
3632 real_file0_str
= vfs_path_to_str (real_file0
);
3633 real_file1_str
= vfs_path_to_str (real_file1
);
3634 file0_str
= vfs_path_to_str (file0
);
3635 file1_str
= vfs_path_to_str (file1
);
3636 rv
= diff_view (real_file0_str
, real_file1_str
, file0_str
, file1_str
);
3637 g_free (real_file0_str
);
3638 g_free (real_file1_str
);
3648 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3651 vfs_path_free (file1
);
3652 vfs_path_free (file0
);
3657 #undef GET_FILE_AND_STAMP
3660 /* --------------------------------------------------------------------------------------------- */