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/history.h"
59 #include "src/selcodepage.h"
65 /*** global variables ****************************************************************************/
67 /*** file scope macro definitions ****************************************************************/
69 #define g_array_foreach(a, TP, cbf) \
71 size_t g_array_foreach_i;\
72 TP *g_array_foreach_var = NULL; \
73 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
75 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
76 (*cbf) (g_array_foreach_var); \
80 #define FILE_READ_BUF 4096
81 #define FILE_FLAG_TEMP (1 << 0)
91 #define HDIFF_ENABLE 1
92 #define HDIFF_MINCTX 5
93 #define HDIFF_DEPTH 10
95 #define FILE_DIRTY(fs) \
103 /*** file scope type declarations ****************************************************************/
109 } action_direction_t
;
111 /*** file scope variables ************************************************************************/
113 /*** file scope functions ************************************************************************/
114 /* --------------------------------------------------------------------------------------------- */
117 TAB_SKIP (int ts
, int pos
)
119 if (ts
> 0 && ts
< 9)
120 return ts
- pos
% ts
;
125 /* --------------------------------------------------------------------------------------------- */
128 rewrite_backup_content (const vfs_path_t
* from_file_name_vpath
, const char *to_file_name
)
133 const char *from_file_name
;
135 from_file_name
= vfs_path_get_by_index (from_file_name_vpath
, -1)->path
;
136 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
139 backup_fd
= fopen (to_file_name
, "w");
140 if (backup_fd
== NULL
)
146 length
= fwrite ((const void *) contents
, length
, 1, backup_fd
);
154 /* buffered I/O ************************************************************* */
157 * Try to open a temporary file.
158 * @note the name is not altered if this function fails
160 * @param[out] name address of a pointer to store the temporary name
161 * @returns file descriptor on success, negative on error
165 open_temp (void **name
)
168 vfs_path_t
*diff_file_name
= NULL
;
170 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
173 message (D_ERROR
, MSG_ERROR
,
174 _("Cannot create temporary diff file\n%s"), unix_error_string (errno
));
177 *name
= vfs_path_to_str (diff_file_name
);
178 vfs_path_free (diff_file_name
);
182 /* --------------------------------------------------------------------------------------------- */
185 * Alocate file structure and associate file descriptor to it.
187 * @param fd file descriptor
188 * @returns file structure
199 fs
= g_try_malloc (sizeof (FBUF
));
203 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
218 /* --------------------------------------------------------------------------------------------- */
221 * Free file structure without closing the file.
223 * @param fs file structure
224 * @returns 0 on success, non-zero on error
232 if (fs
->flags
& FILE_FLAG_TEMP
)
234 rv
= unlink (fs
->data
);
242 /* --------------------------------------------------------------------------------------------- */
245 * Open a binary temporary file in R/W mode.
246 * @note the file will be deleted when closed
248 * @returns file structure
261 fd
= open_temp (&fs
->data
);
269 fs
->flags
= FILE_FLAG_TEMP
;
273 /* --------------------------------------------------------------------------------------------- */
276 * Open a binary file in specified mode.
278 * @param filename file name
279 * @param flags open mode, a combination of O_RDONLY, O_WRONLY, O_RDWR
281 * @returns file structure
285 f_open (const char *filename
, int flags
)
294 fd
= open (filename
, flags
);
305 /* --------------------------------------------------------------------------------------------- */
308 * Read a line of bytes from file until newline or EOF.
309 * @note does not stop on null-byte
310 * @note buf will not be null-terminated
312 * @param buf destination buffer
313 * @param size size of buffer
314 * @param fs file structure
316 * @returns number of bytes read
321 f_gets (char *buf
, size_t size
, FBUF
* fs
)
330 for (i
= fs
->pos
; j
< size
&& i
< fs
->len
&& !stop
; i
++, j
++)
338 if (j
== size
|| stop
)
342 fs
->len
= read (fs
->fd
, fs
->buf
, FILE_READ_BUF
);
349 /* --------------------------------------------------------------------------------------------- */
353 * @note avoids thrashing read cache when possible
355 * @param fs file structure
357 * @param whence seek directive: SEEK_SET, SEEK_CUR or SEEK_END
359 * @returns position in file, starting from begginning
364 f_seek (FBUF
* fs
, off_t off
, int whence
)
368 if (fs
->len
&& whence
!= SEEK_END
)
370 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
373 if (whence
== SEEK_CUR
)
376 off
+= rv
- fs
->len
+ fs
->pos
;
378 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
380 fs
->pos
= fs
->len
+ off
- rv
;
386 rv
= lseek (fs
->fd
, off
, whence
);
392 /* --------------------------------------------------------------------------------------------- */
395 * Seek to the beginning of file, thrashing read cache.
397 * @param fs file structure
399 * @returns 0 if success, non-zero on error
407 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
413 /* --------------------------------------------------------------------------------------------- */
416 * Write bytes to file.
417 * @note thrashes read cache
419 * @param fs file structure
420 * @param buf source buffer
421 * @param size size of buffer
423 * @returns number of written bytes, -1 on error
428 f_write (FBUF
* fs
, const char *buf
, size_t size
)
432 rv
= write (fs
->fd
, buf
, size
);
438 /* --------------------------------------------------------------------------------------------- */
441 * Truncate file to the current position.
442 * @note thrashes read cache
444 * @param fs file structure
446 * @returns current file size on success, negative on error
455 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
460 rv
= ftruncate (fs
->fd
, off
);
469 /* --------------------------------------------------------------------------------------------- */
473 * @note if this is temporary file, it is deleted
475 * @param fs file structure
476 * @returns 0 on success, non-zero on error
494 /* --------------------------------------------------------------------------------------------- */
497 * Create pipe stream to process.
499 * @param cmd shell command line
500 * @param flags open mode, either O_RDONLY or O_WRONLY
502 * @returns file structure
506 p_open (const char *cmd
, int flags
)
510 const char *type
= NULL
;
512 if (flags
== O_RDONLY
)
514 else if (flags
== O_WRONLY
)
524 f
= popen (cmd
, type
);
536 /* --------------------------------------------------------------------------------------------- */
541 * @param fs structure
542 * @returns 0 on success, non-zero on error
552 rv
= pclose (fs
->data
);
559 /* --------------------------------------------------------------------------------------------- */
562 * Get one char (byte) from string
564 * @param char * str, gboolean * result
565 * @returns int as character or 0 and result == FALSE if fail
569 dview_get_byte (char *str
, gboolean
* result
)
577 return (unsigned char) *str
;
580 /* --------------------------------------------------------------------------------------------- */
583 * Get utf multibyte char from string
585 * @param char * str, int * char_width, gboolean * result
586 * @returns int as utf character or 0 and result == FALSE if fail
591 dview_get_utf (char *str
, int *char_width
, gboolean
* result
)
595 gchar
*next_ch
= NULL
;
606 res
= g_utf8_get_char_validated (str
, -1);
613 /* Calculate UTF-8 char width */
614 next_ch
= g_utf8_next_char (str
);
616 width
= next_ch
- str
;
624 /* --------------------------------------------------------------------------------------------- */
627 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
631 if (text
== NULL
|| text
[0] == '\0')
634 if (g_utf8_validate (text
, -1, NULL
))
635 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
639 char *tmpbuf
, *buffer
;
641 buffer
= tmpbuf
= g_strdup (text
);
642 while (tmpbuf
[0] != '\0')
644 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
645 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
646 tmpbuf
= g_utf8_next_char (tmpbuf
);
653 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
656 return max (length
, (size_t) result
);
659 /* --------------------------------------------------------------------------------------------- */
661 /* diff parse *************************************************************** */
664 * Read decimal number from string.
666 * @param[in,out] str string to parse
667 * @param[out] n extracted number
668 * @returns 0 if success, otherwise non-zero
672 scan_deci (const char **str
, int *n
)
674 const char *p
= *str
;
678 *n
= strtol (p
, &q
, 10);
679 if (errno
!= 0 || p
== q
)
685 /* --------------------------------------------------------------------------------------------- */
688 * Parse line for diff statement.
690 * @param p string to parse
691 * @param ops list of diff statements
692 * @returns 0 if success, otherwise non-zero
696 scan_line (const char *p
, GArray
* ops
)
705 /* handle the following cases:
707 * NUM[,NUM]cNUM[,NUM]
709 * where NUM is a positive integer
712 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
720 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
732 else if (cmd
!= 'c' && cmd
!= 'd')
735 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
743 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
749 if (cmd
== 'd' && range
!= 0)
757 g_array_append_val (ops
, op
);
761 /* --------------------------------------------------------------------------------------------- */
764 * Parse diff output and extract diff statements.
766 * @param f stream to read from
767 * @param ops list of diff statements to fill
768 * @returns positive number indicating number of hunks, otherwise negative
772 scan_diff (FBUF
* f
, GArray
* ops
)
777 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
779 if (isdigit (buf
[0]))
781 if (buf
[sz
- 1] != '\n')
785 if (scan_line (buf
, ops
) != 0)
791 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
798 /* --------------------------------------------------------------------------------------------- */
801 * Invoke diff and extract diff statements.
803 * @param args extra arguments to be passed to diff
804 * @param extra more arguments to be passed to diff
805 * @param file1 first file to compare
806 * @param file2 second file to compare
807 * @param ops list of diff statements to fill
809 * @returns positive number indicating number of hunks, otherwise negative
813 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
816 static const char *opt
=
817 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
818 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
819 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
820 " --unchanged-group-format=''";
826 char *file1_esc
, *file2_esc
;
828 /* escape potential $ to avoid shell variable substitutions in popen() */
829 file1_esc
= strutils_shell_escape (file1
);
830 file2_esc
= strutils_shell_escape (file2
);
831 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
838 f
= p_open (cmd
, O_RDONLY
);
844 rv
= scan_diff (f
, ops
);
847 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
853 /* --------------------------------------------------------------------------------------------- */
856 * Reparse and display file according to diff statements.
858 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
859 * @param filename file name to display
860 * @param ops list of diff statements
861 * @param printer printf-like function to be used for displaying
862 * @param ctx printer context
864 * @returns 0 if success, otherwise non-zero
868 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
881 f
= f_open (filename
, O_RDONLY
);
897 #define T1 a[ ord^1 ][0]
898 #define T2 a[ ord^1 ][1]
899 for (i
= 0; i
< ops
->len
; i
++)
903 op
= &g_array_index (ops
, DIFFCMD
, i
);
904 n
= op
->F1
- (op
->cmd
!= add_cmd
);
906 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
909 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
911 while (buf
[sz
- 1] != '\n')
913 sz
= f_gets (buf
, sizeof (buf
), f
);
916 printer (ctx
, 0, 0, 0, 1, "\n");
919 printer (ctx
, 0, 0, 0, sz
, buf
);
927 if (op
->cmd
== add_cmd
)
929 n
= op
->T2
- op
->T1
+ 1;
932 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
937 if (op
->cmd
== del_cmd
)
939 n
= op
->F2
- op
->F1
+ 1;
940 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
943 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
945 while (buf
[sz
- 1] != '\n')
947 sz
= f_gets (buf
, sizeof (buf
), f
);
950 printer (ctx
, 0, 0, 0, 1, "\n");
953 printer (ctx
, 0, 0, 0, sz
, buf
);
965 n
= op
->F2
- op
->F1
+ 1;
966 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
969 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
971 while (buf
[sz
- 1] != '\n')
973 sz
= f_gets (buf
, sizeof (buf
), f
);
976 printer (ctx
, 0, 0, 0, 1, "\n");
979 printer (ctx
, 0, 0, 0, sz
, buf
);
988 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
991 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
1001 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
1004 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
1006 while (buf
[sz
- 1] != '\n')
1008 sz
= f_gets (buf
, sizeof (buf
), f
);
1011 printer (ctx
, 0, 0, 0, 1, "\n");
1014 printer (ctx
, 0, 0, 0, sz
, buf
);
1027 /* --------------------------------------------------------------------------------------------- */
1029 /* horizontal diff ********************************************************** */
1032 * Longest common substring.
1034 * @param s first string
1035 * @param m length of first string
1036 * @param t second string
1037 * @param n length of second string
1038 * @param ret list of offsets for longest common substrings inside each string
1039 * @param min minimum length of common substrings
1041 * @returns 0 if success, nonzero otherwise
1045 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1051 if (m
< min
|| n
< min
)
1053 /* XXX early culling */
1057 Lprev
= g_try_new0 (int, n
+ 1);
1061 Lcurr
= g_try_new0 (int, n
+ 1);
1068 for (i
= 0; i
< m
; i
++)
1075 #ifdef USE_MEMSET_IN_LCS
1076 memset (Lcurr
, 0, (n
+ 1) * sizeof (int));
1078 for (j
= 0; j
< n
; j
++)
1080 #ifndef USE_MEMSET_IN_LCS
1092 g_array_set_size (ret
, 0);
1094 if (z
== v
&& z
>= min
)
1102 for (k
= 0; k
< ret
->len
; k
++)
1104 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1105 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1114 g_array_append_val (ret
, p2
);
1126 /* --------------------------------------------------------------------------------------------- */
1129 * Scan recursively for common substrings and build ranges.
1131 * @param s first string
1132 * @param t second string
1133 * @param bracket current limits for both of the strings
1134 * @param min minimum length of common substrings
1135 * @param hdiff list of horizontal diff ranges to fill
1136 * @param depth recursion depth
1138 * @returns 0 if success, nonzero otherwise
1142 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1153 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1157 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1158 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1162 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1165 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1166 b
[DIFF_LEFT
].len
= (*data
)[0];
1167 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1168 b
[DIFF_RIGHT
].len
= (*data
)[1];
1169 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1172 for (k
= 0; k
< ret
->len
- 1; k
++)
1174 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1175 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1176 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1177 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1178 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1179 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1180 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1183 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1184 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1185 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1186 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1187 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1188 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1191 g_array_free (ret
, TRUE
);
1196 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1197 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1198 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1199 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1200 g_array_append_val (hdiff
, p
);
1205 /* --------------------------------------------------------------------------------------------- */
1208 * Build list of horizontal diff ranges.
1210 * @param s first string
1211 * @param m length of first string
1212 * @param t second string
1213 * @param n length of second string
1214 * @param min minimum length of common substrings
1215 * @param hdiff list of horizontal diff ranges to fill
1216 * @param depth recursion depth
1218 * @returns 0 if success, nonzero otherwise
1222 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1227 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1228 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1230 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1233 b
[DIFF_LEFT
].off
= i
;
1234 b
[DIFF_LEFT
].len
= m
- i
;
1235 b
[DIFF_RIGHT
].off
= i
;
1236 b
[DIFF_RIGHT
].len
= n
- i
;
1238 /* smartscan (multiple horizontal diff) */
1239 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1242 /* --------------------------------------------------------------------------------------------- */
1244 /* read line **************************************************************** */
1247 * Check if character is inside horizontal diff limits.
1249 * @param k rank of character inside line
1250 * @param hdiff horizontal diff structure
1251 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1253 * @returns TRUE if inside hdiff limits, FALSE otherwise
1257 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1262 for (i
= 0; i
< hdiff
->len
; i
++)
1266 b
= &g_array_index (hdiff
, BRACKET
, i
);
1267 start
= (*b
)[ord
].off
;
1268 end
= start
+ (*b
)[ord
].len
;
1269 if (k
>= start
&& k
< end
)
1275 /* --------------------------------------------------------------------------------------------- */
1278 * Copy 'src' to 'dst' expanding tabs.
1279 * @note The procedure returns when all bytes are consumed from 'src'
1281 * @param dst destination buffer
1282 * @param src source buffer
1283 * @param srcsize size of src buffer
1284 * @param base virtual base of this string, needed to calculate tabs
1285 * @param ts tab size
1287 * @returns new virtual base
1291 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1295 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1302 j
= TAB_SKIP (ts
, i
+ base
);
1312 /* --------------------------------------------------------------------------------------------- */
1315 * Copy 'src' to 'dst' expanding tabs.
1317 * @param dst destination buffer
1318 * @param dstsize size of dst buffer
1319 * @param[in,out] _src source buffer
1320 * @param srcsize size of src buffer
1321 * @param base virtual base of this string, needed to calculate tabs
1322 * @param ts tab size
1324 * @returns new virtual base
1326 * @note The procedure returns when all bytes are consumed from 'src'
1327 * or 'dstsize' bytes are written to 'dst'
1328 * @note Upon return, 'src' points to the first unwritten character in source
1332 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1335 const char *src
= *_src
;
1337 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1344 j
= TAB_SKIP (ts
, i
+ base
);
1345 if (j
> dstsize
- i
)
1357 /* --------------------------------------------------------------------------------------------- */
1360 * Read line from memory, converting tabs to spaces and padding with spaces.
1362 * @param src buffer to read from
1363 * @param srcsize size of src buffer
1364 * @param dst buffer to read to
1365 * @param dstsize size of dst buffer, excluding trailing null
1366 * @param skip number of characters to skip
1367 * @param ts tab size
1368 * @param show_cr show trailing carriage return as ^M
1370 * @returns negative on error, otherwise number of bytes except padding
1374 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
)
1384 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1390 j
= TAB_SKIP (ts
, i
+ base
);
1396 else if (dstsize
!= 0)
1403 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1405 if (skip
== 0 && show_cr
)
1428 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1431 if (!g_unichar_isprint (utf_ch
))
1442 while (dstsize
!= 0)
1451 /* --------------------------------------------------------------------------------------------- */
1454 * Read line from memory and build attribute array.
1456 * @param src buffer to read from
1457 * @param srcsize size of src buffer
1458 * @param dst buffer to read to
1459 * @param dstsize size of dst buffer, excluding trailing null
1460 * @param skip number of characters to skip
1461 * @param ts tab size
1462 * @param show_cr show trailing carriage return as ^M
1463 * @param hdiff horizontal diff structure
1464 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1465 * @param att buffer of attributes
1467 * @returns negative on error, otherwise number of bytes except padding
1471 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
, int show_cr
,
1472 GArray
* hdiff
, diff_place_t ord
, char *att
)
1482 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1488 j
= TAB_SKIP (ts
, i
+ base
);
1494 else if (dstsize
!= 0)
1497 *att
++ = is_inside (k
, hdiff
, ord
);
1502 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1504 if (skip
== 0 && show_cr
)
1509 *att
++ = is_inside (k
, hdiff
, ord
);
1511 *att
++ = is_inside (k
, hdiff
, ord
);
1517 *att
++ = is_inside (k
, hdiff
, ord
);
1530 utf_ch
= dview_get_utf ((char *) src
, &w
, &res
);
1533 if (!g_unichar_isprint (utf_ch
))
1539 *att
++ = is_inside (k
, hdiff
, ord
);
1545 while (dstsize
!= 0)
1555 /* --------------------------------------------------------------------------------------------- */
1558 * Read line from file, converting tabs to spaces and padding with spaces.
1560 * @param f file stream to read from
1561 * @param off offset of line inside file
1562 * @param dst buffer to read to
1563 * @param dstsize size of dst buffer, excluding trailing null
1564 * @param skip number of characters to skip
1565 * @param ts tab size
1566 * @param show_cr show trailing carriage return as ^M
1568 * @returns negative on error, otherwise number of bytes except padding
1572 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, int show_cr
)
1575 int old_base
= base
;
1576 size_t amount
= dstsize
;
1577 size_t useful
, offset
;
1581 const char *q
= NULL
;
1582 char tmp
[BUFSIZ
]; /* XXX capacity must be >= max{dstsize + 1, amount} */
1583 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1585 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1587 /* abnormal, but avoid buffer overflow */
1588 memset (dst
, ' ', dstsize
);
1589 dst
[dstsize
] = '\0';
1593 f_seek (f
, off
, SEEK_SET
);
1598 sz
= f_gets (tmp
, amount
, f
);
1602 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1603 if (cvt
[base
- old_base
- 1] == '\n')
1605 q
= &cvt
[base
- old_base
- 1];
1606 base
= old_base
+ q
- cvt
+ 1;
1613 memset (dst
, ' ', dstsize
);
1614 dst
[dstsize
] = '\0';
1618 useful
= base
- skip
;
1619 offset
= skip
- old_base
;
1621 if (useful
<= dstsize
)
1624 memmove (dst
, cvt
+ offset
, useful
);
1628 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1631 const char *ptr
= tmp
;
1633 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1642 memmove (dst
, cvt
+ offset
, dstsize
);
1644 lastch
= cvt
[offset
+ dstsize
];
1648 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1650 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1654 if (i
+ 1 < dstsize
)
1668 for (; i
< dstsize
; i
++)
1674 /* --------------------------------------------------------------------------------------------- */
1675 /* diff printers et al ****************************************************** */
1678 cc_free_elt (void *elt
)
1686 /* --------------------------------------------------------------------------------------------- */
1689 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1691 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1692 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1702 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1704 if (sz
!= 0 && str
[sz
- 1] == '\n')
1707 p
.p
= g_strndup (str
, sz
);
1710 g_array_append_val (a
, p
);
1712 else if (dsrc
== DATA_SRC_MEM
)
1716 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1717 if (sz
!= 0 && str
[sz
- 1] == '\n')
1724 new_size
= p
->u
.len
+ sz
;
1725 q
= g_realloc (p
->p
, new_size
);
1726 memcpy (q
+ p
->u
.len
, str
, sz
);
1731 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1733 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1734 f_write (f
, str
, sz
);
1739 /* --------------------------------------------------------------------------------------------- */
1742 redo_diff (WDiff
* dview
)
1744 FBUF
*const *f
= dview
->f
;
1752 if (dview
->opt
.quality
== 2)
1753 strcat (extra
, " -d");
1754 if (dview
->opt
.quality
== 1)
1755 strcat (extra
, " --speed-large-files");
1756 if (dview
->opt
.strip_trailing_cr
)
1757 strcat (extra
, " --strip-trailing-cr");
1758 if (dview
->opt
.ignore_tab_expansion
)
1759 strcat (extra
, " -E");
1760 if (dview
->opt
.ignore_space_change
)
1761 strcat (extra
, " -b");
1762 if (dview
->opt
.ignore_all_space
)
1763 strcat (extra
, " -w");
1764 if (dview
->opt
.ignore_case
)
1765 strcat (extra
, " -i");
1767 if (dview
->dsrc
!= DATA_SRC_MEM
)
1769 f_reset (f
[DIFF_LEFT
]);
1770 f_reset (f
[DIFF_RIGHT
]);
1773 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1774 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1778 g_array_free (ops
, TRUE
);
1782 ctx
.dsrc
= dview
->dsrc
;
1785 ctx
.a
= dview
->a
[DIFF_LEFT
];
1786 ctx
.f
= f
[DIFF_LEFT
];
1787 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1789 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1790 ctx
.f
= f
[DIFF_RIGHT
];
1791 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1794 g_array_free (ops
, TRUE
);
1796 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1799 if (dview
->dsrc
== DATA_SRC_TMP
)
1801 f_trunc (f
[DIFF_LEFT
]);
1802 f_trunc (f
[DIFF_RIGHT
]);
1805 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1807 dview
->hdiff
= g_ptr_array_new ();
1808 if (dview
->hdiff
!= NULL
)
1814 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1818 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1819 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1820 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1822 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1828 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1832 g_array_free (h
, TRUE
);
1837 g_ptr_array_add (dview
->hdiff
, h
);
1844 /* --------------------------------------------------------------------------------------------- */
1847 destroy_hdiff (WDiff
* dview
)
1849 if (dview
->hdiff
!= NULL
)
1854 len
= dview
->a
[DIFF_LEFT
]->len
;
1856 for (i
= 0; i
< len
; i
++)
1860 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1862 g_array_free (h
, TRUE
);
1864 g_ptr_array_free (dview
->hdiff
, TRUE
);
1865 dview
->hdiff
= NULL
;
1868 mc_search_free (dview
->search
.handle
);
1869 dview
->search
.handle
= NULL
;
1870 g_free (dview
->search
.last_string
);
1871 dview
->search
.last_string
= NULL
;
1874 /* --------------------------------------------------------------------------------------------- */
1875 /* stuff ******************************************************************** */
1878 get_digits (unsigned int n
)
1887 /* --------------------------------------------------------------------------------------------- */
1890 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1902 p
= &g_array_index (a
, DIFFLN
, pos
);
1908 for (n
= pos
; n
> 0; n
--)
1914 *lineofs
= pos
- n
+ 1;
1922 /* --------------------------------------------------------------------------------------------- */
1925 calc_nwidth (const GArray
** const a
)
1930 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1931 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1934 return get_digits (l1
);
1937 /* --------------------------------------------------------------------------------------------- */
1940 find_prev_hunk (const GArray
* a
, int pos
)
1943 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1945 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1947 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1949 if (pos
> 0 && (size_t) pos
< a
->len
)
1952 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1954 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1961 /* --------------------------------------------------------------------------------------------- */
1964 find_next_hunk (const GArray
* a
, size_t pos
)
1966 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1968 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1973 /* --------------------------------------------------------------------------------------------- */
1975 * Find start and end lines of the current hunk.
1977 * @param dview WDiff widget
1978 * @returns boolean and
1979 * start_line1 first line of current hunk (file[0])
1980 * end_line1 last line of current hunk (file[0])
1981 * start_line1 first line of current hunk (file[0])
1982 * end_line1 last line of current hunk (file[0])
1986 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1988 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1989 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1999 pos
= dview
->skip_rows
;
2000 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2015 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2019 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2020 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2022 pos
= dview
->skip_rows
;
2023 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2027 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2028 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2030 *end_line1
= max (*start_line1
, l0
);
2032 *end_line2
= max (*start_line2
, l1
);
2039 /* --------------------------------------------------------------------------------------------- */
2041 * Remove hunk from file.
2043 * @param dview WDiff widget
2044 * @param merge_file file stream for writing data
2045 * @param from1 first line of hunk
2046 * @param to1 last line of hunk
2047 * @param merge_direction in what direction files should be merged
2051 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2052 action_direction_t merge_direction
)
2058 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2059 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2061 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2064 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2067 fputs (buf
, merge_file
);
2069 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2073 fputs (buf
, merge_file
);
2078 /* --------------------------------------------------------------------------------------------- */
2082 * @param dview WDiff widget
2083 * @param merge_file file stream for writing data
2084 * @param from1 first line of source hunk
2085 * @param from2 first line of destination hunk
2086 * @param to1 last line of source hunk
2087 * @param merge_direction in what direction files should be merged
2091 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2092 action_direction_t merge_direction
)
2099 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2101 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2102 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2106 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2107 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2111 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2114 fputs (buf
, merge_file
);
2117 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2121 fputs (buf
, merge_file
);
2123 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2124 fputs (buf
, merge_file
);
2130 /* --------------------------------------------------------------------------------------------- */
2132 * Replace hunk in file.
2134 * @param dview WDiff widget
2135 * @param merge_file file stream for writing data
2136 * @param from1 first line of source hunk
2137 * @param to1 last line of source hunk
2138 * @param from2 first line of destination hunk
2139 * @param to2 last line of destination hunk
2140 * @param merge_direction in what direction files should be merged
2144 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2145 action_direction_t merge_direction
)
2147 int line1
= 0, line2
= 0;
2152 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2154 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2155 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2159 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2160 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2163 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2166 fputs (buf
, merge_file
);
2168 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2172 fputs (buf
, merge_file
);
2174 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2178 fputs (buf
, merge_file
);
2184 /* --------------------------------------------------------------------------------------------- */
2188 * @param dview WDiff widget
2189 * @param merge_direction in what direction files should be merged
2193 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2195 int from1
, to1
, from2
, to2
;
2198 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2200 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2201 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2203 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2209 vfs_path_t
*merge_file_name_vpath
= NULL
;
2211 if (!dview
->merged
[n_merge
])
2213 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2214 if (!dview
->merged
[n_merge
])
2216 message (D_ERROR
, MSG_ERROR
,
2217 _("Cannot create backup file\n%s%s\n%s"),
2218 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2223 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2224 if (merge_file_fd
== -1)
2226 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2227 unix_error_string (errno
));
2231 merge_file
= fdopen (merge_file_fd
, "w");
2236 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2237 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2239 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2242 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2243 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2245 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2248 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2251 fflush (merge_file
);
2252 fclose (merge_file
);
2253 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2254 mc_unlink (merge_file_name_vpath
);
2255 vfs_path_free (merge_file_name_vpath
);
2259 /* --------------------------------------------------------------------------------------------- */
2260 /* view routines and callbacks ********************************************** */
2263 dview_compute_split (WDiff
* dview
, int i
)
2266 if (dview
->bias
< 2 - dview
->half1
)
2267 dview
->bias
= 2 - dview
->half1
;
2268 if (dview
->bias
> dview
->half2
- 2)
2269 dview
->bias
= dview
->half2
- 2;
2272 /* --------------------------------------------------------------------------------------------- */
2275 dview_compute_areas (WDiff
* dview
)
2277 dview
->height
= LINES
- 2;
2278 dview
->half1
= COLS
/ 2;
2279 dview
->half2
= COLS
- dview
->half1
;
2281 dview_compute_split (dview
, 0);
2284 /* --------------------------------------------------------------------------------------------- */
2287 dview_reread (WDiff
* dview
)
2291 destroy_hdiff (dview
);
2292 if (dview
->a
[DIFF_LEFT
] != NULL
)
2294 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2295 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2297 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2299 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2300 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2303 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2304 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2306 ndiff
= redo_diff (dview
);
2308 dview
->ndiff
= ndiff
;
2311 /* --------------------------------------------------------------------------------------------- */
2315 dview_set_codeset (WDiff
* dview
)
2317 const char *encoding_id
= NULL
;
2321 get_codepage_id (mc_global
.source_codepage
>=
2322 0 ? mc_global
.source_codepage
: mc_global
.display_codepage
);
2323 if (encoding_id
!= NULL
)
2327 conv
= str_crt_conv_from (encoding_id
);
2328 if (conv
!= INVALID_CONV
)
2330 if (dview
->converter
!= str_cnv_from_term
)
2331 str_close_conv (dview
->converter
);
2332 dview
->converter
= conv
;
2334 dview
->utf8
= (gboolean
) str_isutf8 (encoding_id
);
2338 /* --------------------------------------------------------------------------------------------- */
2341 dview_select_encoding (WDiff
* dview
)
2343 if (do_select_codepage ())
2344 dview_set_codeset (dview
);
2345 dview_reread (dview
);
2346 tty_touch_screen ();
2349 #endif /* HAVE_CHARSET */
2351 /* --------------------------------------------------------------------------------------------- */
2354 dview_diff_options (WDiff
* dview
)
2356 const char *quality_str
[] = {
2358 N_("&Fastest (Assume large files)"),
2359 N_("&Minimal (Find a smaller set of change)")
2362 QuickWidget diffopt_widgets
[] = {
2363 QUICK_BUTTON (6, 10, 14, OPTY
, N_("&Cancel"), B_CANCEL
, NULL
),
2364 QUICK_BUTTON (2, 10, 14, OPTY
, N_("&OK"), B_ENTER
, NULL
),
2366 QUICK_CHECKBOX (3, OPTX
, 12, OPTY
,
2367 N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
),
2368 QUICK_CHECKBOX (3, OPTX
, 11, OPTY
,
2369 N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
),
2370 QUICK_CHECKBOX (3, OPTX
, 10, OPTY
,
2371 N_("Ignore &space change"), &dview
->opt
.ignore_space_change
),
2372 QUICK_CHECKBOX (3, OPTX
, 9, OPTY
,
2373 N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
),
2374 QUICK_CHECKBOX (3, OPTX
, 8, OPTY
,
2375 N_("&Ignore case"), &dview
->opt
.ignore_case
),
2376 QUICK_LABEL (3, OPTX
, 7, OPTY
, N_("Diff extra options")),
2377 QUICK_RADIO (3, OPTX
, 3, OPTY
,
2378 3, (const char **) quality_str
, (int *) &dview
->opt
.quality
),
2379 QUICK_LABEL (3, OPTX
, 2, OPTY
, N_("Diff algorithm")),
2384 QuickDialog diffopt
= {
2386 N_("Diff Options"), "[Diff Options]",
2387 diffopt_widgets
, NULL
, NULL
, FALSE
2390 if (quick_dialog (&diffopt
) != B_CANCEL
)
2391 dview_reread (dview
);
2394 /* --------------------------------------------------------------------------------------------- */
2397 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2398 const char *label1
, const char *label2
, DSRC dsrc
)
2401 FBUF
*f
[DIFF_COUNT
];
2403 f
[DIFF_LEFT
] = NULL
;
2404 f
[DIFF_RIGHT
] = NULL
;
2406 if (dsrc
== DATA_SRC_TMP
)
2408 f
[DIFF_LEFT
] = f_temp ();
2409 if (f
[DIFF_LEFT
] == NULL
)
2412 f
[DIFF_RIGHT
] = f_temp ();
2413 if (f
[DIFF_RIGHT
] == NULL
)
2415 f_close (f
[DIFF_LEFT
]);
2419 else if (dsrc
== DATA_SRC_ORG
)
2421 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2422 if (f
[DIFF_LEFT
] == NULL
)
2425 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2426 if (f
[DIFF_RIGHT
] == NULL
)
2428 f_close (f
[DIFF_LEFT
]);
2434 dview
->file
[DIFF_LEFT
] = file1
;
2435 dview
->file
[DIFF_RIGHT
] = file2
;
2436 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2437 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2438 dview
->f
[DIFF_LEFT
] = f
[0];
2439 dview
->f
[DIFF_RIGHT
] = f
[1];
2440 dview
->merged
[DIFF_LEFT
] = FALSE
;
2441 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2442 dview
->hdiff
= NULL
;
2444 dview
->converter
= str_cnv_from_term
;
2446 dview_set_codeset (dview
);
2448 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2449 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2451 ndiff
= redo_diff (dview
);
2454 /* goto WIDGET_DESTROY stage: dview_fini() */
2455 f_close (f
[DIFF_LEFT
]);
2456 f_close (f
[DIFF_RIGHT
]);
2460 dview
->ndiff
= ndiff
;
2462 dview
->view_quit
= 0;
2465 dview
->new_frame
= 1;
2466 dview
->skip_rows
= 0;
2467 dview
->skip_cols
= 0;
2468 dview
->display_symbols
= 0;
2469 dview
->display_numbers
= 0;
2471 dview
->tab_size
= 8;
2472 dview
->ord
= DIFF_LEFT
;
2475 dview
->search
.handle
= NULL
;
2476 dview
->search
.last_string
= NULL
;
2477 dview
->search
.last_found_line
= -1;
2478 dview
->search
.last_accessed_num_line
= -1;
2480 dview
->opt
.quality
= 0;
2481 dview
->opt
.strip_trailing_cr
= 0;
2482 dview
->opt
.ignore_tab_expansion
= 0;
2483 dview
->opt
.ignore_space_change
= 0;
2484 dview
->opt
.ignore_all_space
= 0;
2485 dview
->opt
.ignore_case
= 0;
2487 dview_compute_areas (dview
);
2492 /* --------------------------------------------------------------------------------------------- */
2495 dview_fini (WDiff
* dview
)
2497 if (dview
->dsrc
!= DATA_SRC_MEM
)
2499 f_close (dview
->f
[DIFF_RIGHT
]);
2500 f_close (dview
->f
[DIFF_LEFT
]);
2503 if (dview
->converter
!= str_cnv_from_term
)
2504 str_close_conv (dview
->converter
);
2506 destroy_hdiff (dview
);
2507 if (dview
->a
[DIFF_LEFT
] != NULL
)
2509 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2510 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2511 dview
->a
[DIFF_LEFT
] = NULL
;
2513 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2515 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2516 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2517 dview
->a
[DIFF_RIGHT
] = NULL
;
2520 g_free (dview
->label
[DIFF_LEFT
]);
2521 g_free (dview
->label
[DIFF_RIGHT
]);
2524 /* --------------------------------------------------------------------------------------------- */
2527 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2532 FBUF
*f
= dview
->f
[ord
];
2533 int skip
= dview
->skip_cols
;
2534 int display_symbols
= dview
->display_symbols
;
2535 int display_numbers
= dview
->display_numbers
;
2536 int show_cr
= dview
->show_cr
;
2539 int nwidth
= display_numbers
;
2542 xwidth
= display_symbols
+ display_numbers
;
2543 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2544 tab_size
= dview
->tab_size
;
2548 if (xwidth
> width
&& display_symbols
)
2551 display_symbols
= 0;
2553 if (xwidth
> width
&& display_numbers
)
2556 display_numbers
= width
;
2566 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2568 /* abnormal, but avoid buffer overflow */
2572 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2574 int ch
, next_ch
, col
;
2577 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2579 tty_setcolor (NORMAL_COLOR
);
2580 if (display_symbols
)
2582 tty_gotoyx (r
+ j
, c
- 2);
2583 tty_print_char (ch
);
2587 if (display_numbers
)
2589 tty_gotoyx (r
+ j
, c
- xwidth
);
2590 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2591 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2594 tty_setcolor (DFF_ADD_COLOR
);
2596 tty_setcolor (DFF_CHG_COLOR
);
2599 if (i
== (size_t) dview
->search
.last_found_line
)
2600 tty_setcolor (MARKED_SELECTED_COLOR
);
2601 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2606 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2610 cvt_mgeta (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
,
2611 g_ptr_array_index (dview
->hdiff
, i
), ord
, att
);
2612 tty_gotoyx (r
+ j
, c
);
2615 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2622 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2625 if (!g_unichar_isprint (next_ch
))
2629 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2633 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2635 if (mc_global
.utf8_display
)
2640 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2644 else if (dview
->utf8
)
2645 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2647 next_ch
= convert_to_display_c (next_ch
);
2649 tty_print_anychar (next_ch
);
2657 tty_setcolor (DFF_CHH_COLOR
);
2660 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2663 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2666 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2670 if (display_numbers
)
2672 tty_gotoyx (r
+ j
, c
- xwidth
);
2673 memset (buf
, ' ', display_numbers
);
2674 buf
[display_numbers
] = '\0';
2675 tty_print_string (buf
);
2678 tty_setcolor (DFF_DEL_COLOR
);
2680 tty_setcolor (DFF_CHD_COLOR
);
2681 memset (buf
, ' ', width
);
2684 tty_gotoyx (r
+ j
, c
);
2685 /* tty_print_nstring (buf, width); */
2687 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2694 next_ch
= dview_get_utf (buf
+ cnt
, &w
, &ch_res
);
2697 if (!g_unichar_isprint (next_ch
))
2701 next_ch
= dview_get_byte (buf
+ cnt
, &ch_res
);
2705 if (mc_global
.utf8_display
)
2710 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2713 else if (dview
->utf8
)
2714 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2716 next_ch
= convert_to_display_c (next_ch
);
2719 tty_print_anychar (next_ch
);
2724 tty_setcolor (NORMAL_COLOR
);
2726 if (width
< xwidth
- 1)
2728 memset (buf
, ' ', k
);
2730 for (; j
< height
; j
++)
2734 tty_gotoyx (r
+ j
, c
- xwidth
);
2735 /* tty_print_nstring (buf, xwidth - 1); */
2736 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2738 tty_gotoyx (r
+ j
, c
);
2739 /* tty_print_nstring (buf, width); */
2740 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2746 /* --------------------------------------------------------------------------------------------- */
2749 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2753 int linenum
, lineofs
;
2757 tty_setcolor (STATUSBAR_COLOR
);
2760 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2762 filename_width
= width
- 24;
2763 if (filename_width
< 8)
2766 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2767 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2768 vfs_path_free (vpath
);
2769 buf
= str_term_trim (path
, filename_width
);
2770 if (ord
== DIFF_LEFT
)
2771 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2772 buf
, linenum
, lineofs
, dview
->skip_cols
);
2774 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2775 buf
, linenum
, lineofs
, dview
->ndiff
);
2779 /* --------------------------------------------------------------------------------------------- */
2782 dview_redo (WDiff
* dview
)
2784 if (dview
->display_numbers
)
2788 old
= dview
->display_numbers
;
2789 dview
->display_numbers
= calc_nwidth ((const GArray
**) dview
->a
);
2790 dview
->new_frame
= (old
!= dview
->display_numbers
);
2792 dview_reread (dview
);
2795 /* --------------------------------------------------------------------------------------------- */
2798 dview_update (WDiff
* dview
)
2800 int height
= dview
->height
;
2805 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2807 if (dview
->skip_rows
> last
)
2808 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2809 if (dview
->skip_rows
< 0)
2810 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2811 if (dview
->skip_cols
< 0)
2812 dview
->skip_cols
= 0;
2817 width1
= dview
->half1
+ dview
->bias
;
2818 width2
= dview
->half2
- dview
->bias
;
2825 if (dview
->new_frame
)
2829 tty_setcolor (NORMAL_COLOR
);
2830 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2832 tty_draw_box (1, 0, height
, width1
, FALSE
);
2834 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2839 if (xwidth
< width1
- 1)
2841 tty_gotoyx (1, xwidth
);
2842 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2843 tty_gotoyx (height
, xwidth
);
2844 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2845 tty_draw_vline (2, xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2847 if (xwidth
< width2
- 1)
2849 tty_gotoyx (1, width1
+ xwidth
);
2850 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DTOPMIDDLE
], FALSE
);
2851 tty_gotoyx (height
, width1
+ xwidth
);
2852 tty_print_alt_char (mc_tty_frm
[MC_TTY_FRM_DBOTTOMMIDDLE
], FALSE
);
2853 tty_draw_vline (2, width1
+ xwidth
, mc_tty_frm
[MC_TTY_FRM_VERT
], height
- 2);
2856 dview
->new_frame
= 0;
2861 dview_status (dview
, dview
->ord
, width1
, 0);
2862 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2866 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2867 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2871 /* --------------------------------------------------------------------------------------------- */
2874 dview_edit (WDiff
* dview
, diff_place_t ord
)
2878 int linenum
, lineofs
;
2880 if (dview
->dsrc
== DATA_SRC_TMP
)
2882 error_dialog (_("Edit"), _("Edit is disabled"));
2886 h
= ((Widget
*) dview
)->owner
;
2889 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2890 h
->modal
= TRUE
; /* not allow edit file in several editors */
2892 vfs_path_t
*tmp_vpath
;
2894 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2895 do_edit_at_line (tmp_vpath
, use_internal_edit
, linenum
);
2896 vfs_path_free (tmp_vpath
);
2900 dview_update (dview
);
2903 /* --------------------------------------------------------------------------------------------- */
2906 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2909 static const char *title
[2] = {
2910 N_("Goto line (left)"),
2911 N_("Goto line (right)")
2914 static char prev
[256];
2915 /* XXX some statics here, to be remembered between runs */
2920 input
= input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
, prev
);
2923 const char *s
= input
;
2925 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2933 for (; i
< dview
->a
[ord
]->len
; i
++)
2935 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2936 if (p
->line
== newline
)
2940 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2941 g_snprintf (prev
, sizeof (prev
), "%d", newline
);
2947 /* --------------------------------------------------------------------------------------------- */
2950 dview_labels (WDiff
* dview
)
2955 h
= dview
->widget
.owner
;
2956 b
= find_buttonbar (h
);
2958 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, (Widget
*) dview
);
2959 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, (Widget
*) dview
);
2960 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, (Widget
*) dview
);
2961 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, (Widget
*) dview
);
2962 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, (Widget
*) dview
);
2963 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, (Widget
*) dview
);
2964 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, (Widget
*) dview
);
2967 /* --------------------------------------------------------------------------------------------- */
2970 dview_event (Gpm_Event
* event
, void *data
)
2972 WDiff
*dview
= (WDiff
*) data
;
2974 if (!mouse_global_in_widget (event
, data
))
2975 return MOU_UNHANDLED
;
2977 /* We are not interested in release events */
2978 if ((event
->type
& (GPM_DOWN
| GPM_DRAG
)) == 0)
2982 if ((event
->buttons
& GPM_B_UP
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2984 dview
->skip_rows
-= 2;
2985 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2986 dview_update (dview
);
2988 else if ((event
->buttons
& GPM_B_DOWN
) != 0 && (event
->type
& GPM_DOWN
) != 0)
2990 dview
->skip_rows
+= 2;
2991 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
2992 dview_update (dview
);
2998 /* --------------------------------------------------------------------------------------------- */
3001 dview_save (WDiff
* dview
)
3003 gboolean res
= TRUE
;
3005 if (dview
->merged
[DIFF_LEFT
])
3007 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3008 dview
->merged
[DIFF_LEFT
] = !res
;
3010 if (dview
->merged
[DIFF_RIGHT
])
3012 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3013 dview
->merged
[DIFF_RIGHT
] = !res
;
3018 /* --------------------------------------------------------------------------------------------- */
3021 dview_do_save (WDiff
* dview
)
3023 (void) dview_save (dview
);
3026 /* --------------------------------------------------------------------------------------------- */
3029 dview_save_options (WDiff
* dview
)
3031 mc_config_set_bool (mc_main_config
, "DiffView", "show_symbols",
3032 dview
->display_symbols
!= 0 ? TRUE
: FALSE
);
3033 mc_config_set_bool (mc_main_config
, "DiffView", "show_numbers",
3034 dview
->display_numbers
!= 0 ? TRUE
: FALSE
);
3035 mc_config_set_int (mc_main_config
, "DiffView", "tab_size", dview
->tab_size
);
3037 mc_config_set_int (mc_main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3039 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_tws",
3040 dview
->opt
.strip_trailing_cr
);
3041 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_all_space",
3042 dview
->opt
.ignore_all_space
);
3043 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_space_change",
3044 dview
->opt
.ignore_space_change
);
3045 mc_config_set_bool (mc_main_config
, "DiffView", "diff_tab_expansion",
3046 dview
->opt
.ignore_tab_expansion
);
3047 mc_config_set_bool (mc_main_config
, "DiffView", "diff_ignore_case", dview
->opt
.ignore_case
);
3050 /* --------------------------------------------------------------------------------------------- */
3053 dview_load_options (WDiff
* dview
)
3055 gboolean show_numbers
, show_symbols
;
3058 show_symbols
= mc_config_get_bool (mc_main_config
, "DiffView", "show_symbols", FALSE
);
3060 dview
->display_symbols
= 1;
3061 show_numbers
= mc_config_get_bool (mc_main_config
, "DiffView", "show_numbers", FALSE
);
3063 dview
->display_numbers
= calc_nwidth ((const GArray
** const) dview
->a
);
3064 tab_size
= mc_config_get_int (mc_main_config
, "DiffView", "tab_size", 8);
3065 if (tab_size
> 0 && tab_size
< 9)
3066 dview
->tab_size
= tab_size
;
3068 dview
->tab_size
= 8;
3070 dview
->opt
.quality
= mc_config_get_int (mc_main_config
, "DiffView", "diff_quality", 0);
3072 dview
->opt
.strip_trailing_cr
=
3073 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3074 dview
->opt
.ignore_all_space
=
3075 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3076 dview
->opt
.ignore_space_change
=
3077 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3078 dview
->opt
.ignore_tab_expansion
=
3079 mc_config_get_bool (mc_main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3080 dview
->opt
.ignore_case
=
3081 mc_config_get_bool (mc_main_config
, "DiffView", "diff_ignore_case", FALSE
);
3083 dview
->new_frame
= 1;
3086 /* --------------------------------------------------------------------------------------------- */
3089 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3093 dview_ok_to_exit (WDiff
* dview
)
3095 gboolean res
= TRUE
;
3098 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3101 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3102 _("File(s) was modified. Save with exit?") :
3103 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3104 D_NORMAL
, 2, _("&Yes"), _("&No"));
3107 if (mc_global
.midnight_shutdown
|| (act
== -1))
3116 (void) dview_save (dview
);
3120 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3121 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3122 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3123 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3132 /* --------------------------------------------------------------------------------------------- */
3135 dview_execute_cmd (WDiff
* dview
, unsigned long command
)
3137 cb_ret_t res
= MSG_HANDLED
;
3141 case CK_ShowSymbols
:
3142 dview
->display_symbols
^= 1;
3143 dview
->new_frame
= 1;
3145 case CK_ShowNumbers
:
3146 dview
->display_numbers
^= calc_nwidth ((const GArray
** const) dview
->a
);
3147 dview
->new_frame
= 1;
3151 dview
->new_frame
= 1;
3157 dview
->new_frame
= 1;
3163 dview_compute_split (dview
, 1);
3164 dview
->new_frame
= 1;
3171 dview_compute_split (dview
, -1);
3172 dview
->new_frame
= 1;
3176 dview
->tab_size
= 2;
3179 dview
->tab_size
= 3;
3182 dview
->tab_size
= 4;
3185 dview
->tab_size
= 8;
3194 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3195 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3198 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3199 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3202 dview_goto_cmd (dview
, TRUE
);
3205 dview_edit (dview
, dview
->ord
);
3208 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3212 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3216 dview_edit (dview
, dview
->ord
^ 1);
3219 dview_search_cmd (dview
);
3221 case CK_SearchContinue
:
3222 dview_continue_search_cmd (dview
);
3225 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3228 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3231 if (dview
->skip_rows
> 0)
3234 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3239 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3242 if (dview
->height
> 2)
3244 dview
->skip_rows
+= dview
->height
- 2;
3245 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3249 if (dview
->height
> 2)
3251 dview
->skip_rows
-= dview
->height
- 2;
3252 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3262 dview
->skip_cols
-= 8;
3265 dview
->skip_cols
+= 8;
3268 dview
->skip_cols
= 0;
3274 dview
->view_quit
= 1;
3277 dview_do_save (dview
);
3280 dview_diff_options (dview
);
3283 case CK_SelectCodepage
:
3284 dview_select_encoding (dview
);
3288 /* don't close diffviewer due to SIGINT */
3291 res
= MSG_NOT_HANDLED
;
3296 /* --------------------------------------------------------------------------------------------- */
3299 dview_handle_key (WDiff
* dview
, int key
)
3301 unsigned long command
;
3304 key
= convert_from_input_c (key
);
3307 command
= keybind_lookup_keymap_command (diff_map
, key
);
3308 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3312 return MSG_NOT_HANDLED
;
3315 /* --------------------------------------------------------------------------------------------- */
3318 dview_callback (Widget
* w
, widget_msg_t msg
, int parm
)
3320 WDiff
*dview
= (WDiff
*) w
;
3321 Dlg_head
*h
= dview
->widget
.owner
;
3327 dview_labels (dview
);
3328 dview_load_options (dview
);
3329 dview_update (dview
);
3333 dview
->new_frame
= 1;
3334 dview_update (dview
);
3338 i
= dview_handle_key (dview
, parm
);
3339 if (dview
->view_quit
)
3342 dview_update (dview
);
3345 case WIDGET_COMMAND
:
3346 i
= dview_execute_cmd (dview
, parm
);
3347 if (dview
->view_quit
)
3350 dview_update (dview
);
3353 case WIDGET_DESTROY
:
3354 dview_save_options (dview
);
3359 return default_proc (msg
, parm
);
3363 /* --------------------------------------------------------------------------------------------- */
3366 dview_adjust_size (Dlg_head
* h
)
3371 /* Look up the viewer and the buttonbar, we assume only two widgets here */
3372 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3373 bar
= find_buttonbar (h
);
3374 widget_set_size (&dview
->widget
, 0, 0, LINES
- 1, COLS
);
3375 widget_set_size ((Widget
*) bar
, LINES
- 1, 0, 1, COLS
);
3377 dview_compute_areas (dview
);
3380 /* --------------------------------------------------------------------------------------------- */
3383 dview_dialog_callback (Dlg_head
* h
, Widget
* sender
, dlg_msg_t msg
, int parm
, void *data
)
3385 WDiff
*dview
= (WDiff
*) data
;
3390 dview_adjust_size (h
);
3396 return dview_execute_cmd (NULL
, parm
);
3397 /* message from buttonbar */
3398 if (sender
== (Widget
*) find_buttonbar (h
))
3401 return send_message ((Widget
*) data
, WIDGET_COMMAND
, parm
);
3403 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3404 return dview_execute_cmd (dview
, parm
);
3406 return MSG_NOT_HANDLED
;
3409 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3410 h
->state
= DLG_ACTIVE
; /* don't stop the dialog before final decision */
3411 if (dview_ok_to_exit (dview
))
3412 h
->state
= DLG_CLOSED
;
3416 return default_dlg_callback (h
, sender
, msg
, parm
, data
);
3420 /* --------------------------------------------------------------------------------------------- */
3423 dview_get_title (const Dlg_head
* h
, size_t len
)
3426 const char *modified
= " (*) ";
3427 const char *notmodified
= " ";
3431 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3432 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3434 title
= g_string_sized_new (len
);
3435 g_string_append (title
, _("Diff:"));
3436 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3437 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3438 g_string_append (title
, " | ");
3439 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3440 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3442 return g_string_free (title
, FALSE
);
3445 /* --------------------------------------------------------------------------------------------- */
3448 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3452 Dlg_head
*dview_dlg
;
3454 /* Create dialog and widgets, put them on the dialog */
3456 create_dlg (FALSE
, 0, 0, LINES
, COLS
, NULL
, dview_dialog_callback
, NULL
,
3457 "[Diff Viewer]", NULL
, DLG_WANT_TAB
);
3459 dview
= g_new0 (WDiff
, 1);
3461 init_widget (&dview
->widget
, 0, 0, LINES
- 1, COLS
,
3462 (callback_fn
) dview_callback
, (mouse_h
) dview_event
);
3464 widget_want_cursor (dview
->widget
, 0);
3466 add_widget (dview_dlg
, dview
);
3467 add_widget (dview_dlg
, buttonbar_new (TRUE
));
3469 dview_dlg
->get_title
= dview_get_title
;
3471 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3473 /* Please note that if you add another widget,
3474 * you have to modify dview_adjust_size to
3478 run_dlg (dview_dlg
);
3480 if ((error
!= 0) || (dview_dlg
->state
== DLG_CLOSED
))
3481 destroy_dlg (dview_dlg
);
3483 return error
== 0 ? 1 : 0;
3486 /*** public functions ****************************************************************************/
3487 /* --------------------------------------------------------------------------------------------- */
3489 #define GET_FILE_AND_STAMP(n) \
3493 real_file##n = file##n; \
3494 if (!vfs_file_is_local (file##n)) \
3496 real_file##n = mc_getlocalcopy (file##n); \
3497 if (real_file##n != NULL) \
3500 if (mc_stat (real_file##n, &st##n) != 0) \
3507 #define UNGET_FILE(n) \
3513 if (use_copy##n > 0) \
3516 mtime = st##n.st_mtime; \
3517 if (mc_stat (real_file##n, &st##n) == 0) \
3518 changed = (mtime != st##n.st_mtime); \
3520 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3521 vfs_path_free (real_file##n); \
3527 dview_diff_cmd (const void *f0
, const void *f1
)
3530 vfs_path_t
*file0
= NULL
;
3531 vfs_path_t
*file1
= NULL
;
3532 gboolean is_dir0
= FALSE
;
3533 gboolean is_dir1
= FALSE
;
3535 switch (mc_global
.mc_run_mode
)
3539 /* run from panels */
3540 const WPanel
*panel0
= (const WPanel
*) f0
;
3541 const WPanel
*panel1
= (const WPanel
*) f1
;
3543 file0
= vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, NULL
);
3544 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3547 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3548 path_trunc (selection (panel0
)->fname
, 30));
3552 file1
= vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, NULL
);
3553 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3556 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3557 path_trunc (selection (panel1
)->fname
, 30));
3563 case MC_RUN_DIFFVIEWER
:
3565 /* run from command line */
3566 const char *p0
= (const char *) f0
;
3567 const char *p1
= (const char *) f1
;
3570 file0
= vfs_path_from_str (p0
);
3571 if (mc_stat (file0
, &st
) == 0)
3573 is_dir0
= S_ISDIR (st
.st_mode
);
3576 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3582 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3583 path_trunc (p0
, 30), unix_error_string (errno
));
3587 file1
= vfs_path_from_str (p1
);
3588 if (mc_stat (file1
, &st
) == 0)
3590 is_dir1
= S_ISDIR (st
.st_mode
);
3593 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3599 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3600 path_trunc (p1
, 30), unix_error_string (errno
));
3607 /* this should not happaned */
3608 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3615 if (file0
!= NULL
&& file1
!= NULL
)
3621 vfs_path_t
*real_file0
;
3622 vfs_path_t
*real_file1
;
3624 GET_FILE_AND_STAMP (0);
3625 GET_FILE_AND_STAMP (1);
3626 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3628 char *real_file0_str
, *real_file1_str
;
3629 char *file0_str
, *file1_str
;
3631 real_file0_str
= vfs_path_to_str (real_file0
);
3632 real_file1_str
= vfs_path_to_str (real_file1
);
3633 file0_str
= vfs_path_to_str (file0
);
3634 file1_str
= vfs_path_to_str (file1
);
3635 rv
= diff_view (real_file0_str
, real_file1_str
, file0_str
, file1_str
);
3636 g_free (real_file0_str
);
3637 g_free (real_file1_str
);
3647 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3650 vfs_path_free (file1
);
3651 vfs_path_free (file0
);
3656 #undef GET_FILE_AND_STAMP
3659 /* --------------------------------------------------------------------------------------------- */