4 Copyright (C) 2007-2020
5 Free Software Foundation, Inc.
8 Daniel Borca <dborca@yahoo.com>, 2007
9 Slava Zanko <slavazanko@gmail.com>, 2010, 2013
10 Andrew Borodin <aborodin@vmail.ru>, 2010, 2012, 2013, 2016
11 Ilia Maslakov <il.smind@gmail.com>, 2010
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
35 #include <sys/types.h>
38 #include "lib/global.h"
39 #include "lib/tty/tty.h"
40 #include "lib/tty/color.h"
41 #include "lib/tty/key.h"
42 #include "lib/skin.h" /* EDITOR_NORMAL_COLOR */
43 #include "lib/vfs/vfs.h" /* mc_opendir, mc_readdir, mc_closedir, */
45 #include "lib/widget.h"
46 #include "lib/strutil.h"
47 #include "lib/strescape.h" /* strutils_glob_escape() */
49 #include "lib/charsets.h"
51 #include "lib/event.h" /* mc_event_raise() */
53 #include "src/filemanager/cmd.h" /* edit_file_at_line() */
54 #include "src/filemanager/panel.h"
55 #include "src/filemanager/layout.h" /* Needed for get_current_index and get_other_panel */
57 #include "src/execute.h" /* toggle_subshell() */
58 #include "src/keybind-defaults.h"
59 #include "src/setup.h"
60 #include "src/history.h"
62 #include "src/selcodepage.h"
68 /*** global variables ****************************************************************************/
70 /*** file scope macro definitions ****************************************************************/
72 #define g_array_foreach(a, TP, cbf) \
74 size_t g_array_foreach_i;\
76 for (g_array_foreach_i = 0; g_array_foreach_i < a->len; g_array_foreach_i++) \
78 TP *g_array_foreach_var; \
80 g_array_foreach_var = &g_array_index (a, TP, g_array_foreach_i); \
81 (*cbf) (g_array_foreach_var); \
85 #define FILE_READ_BUF 4096
86 #define FILE_FLAG_TEMP (1 << 0)
93 #define HDIFF_ENABLE 1
94 #define HDIFF_MINCTX 5
95 #define HDIFF_DEPTH 10
97 #define FILE_DIRTY(fs) \
105 /*** file scope type declarations ****************************************************************/
111 } action_direction_t
;
113 /*** file scope variables ************************************************************************/
115 /*** file scope functions ************************************************************************/
116 /* --------------------------------------------------------------------------------------------- */
119 TAB_SKIP (int ts
, int pos
)
121 if (ts
> 0 && ts
< 9)
122 return ts
- pos
% ts
;
127 /* --------------------------------------------------------------------------------------------- */
130 rewrite_backup_content (const vfs_path_t
* from_file_name_vpath
, const char *to_file_name
)
135 const char *from_file_name
;
137 from_file_name
= vfs_path_get_by_index (from_file_name_vpath
, -1)->path
;
138 if (!g_file_get_contents (from_file_name
, &contents
, &length
, NULL
))
141 backup_fd
= fopen (to_file_name
, "w");
142 if (backup_fd
== NULL
)
148 length
= fwrite ((const void *) contents
, length
, 1, backup_fd
);
156 /* buffered I/O ************************************************************* */
159 * Try to open a temporary file.
160 * @note the name is not altered if this function fails
162 * @param[out] name address of a pointer to store the temporary name
163 * @return file descriptor on success, negative on error
167 open_temp (void **name
)
170 vfs_path_t
*diff_file_name
= NULL
;
172 fd
= mc_mkstemps (&diff_file_name
, "mcdiff", NULL
);
175 message (D_ERROR
, MSG_ERROR
,
176 _("Cannot create temporary diff file\n%s"), unix_error_string (errno
));
179 *name
= g_strdup (vfs_path_as_str (diff_file_name
));
180 vfs_path_free (diff_file_name
);
184 /* --------------------------------------------------------------------------------------------- */
187 * Alocate file structure and associate file descriptor to it.
189 * @param fd file descriptor
190 * @return file structure
201 fs
= g_try_malloc (sizeof (FBUF
));
205 fs
->buf
= g_try_malloc (FILE_READ_BUF
);
220 /* --------------------------------------------------------------------------------------------- */
223 * Free file structure without closing the file.
225 * @param fs file structure
226 * @return 0 on success, non-zero on error
234 if (fs
->flags
& FILE_FLAG_TEMP
)
236 rv
= unlink (fs
->data
);
244 /* --------------------------------------------------------------------------------------------- */
247 * Open a binary temporary file in R/W mode.
248 * @note the file will be deleted when closed
250 * @return 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 * @return 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 * @return 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 * @return position in file, starting from begginning
363 f_seek (FBUF
* fs
, off_t off
, int whence
)
367 if (fs
->len
&& whence
!= SEEK_END
)
369 rv
= lseek (fs
->fd
, 0, SEEK_CUR
);
372 if (whence
== SEEK_CUR
)
375 off
+= rv
- fs
->len
+ fs
->pos
;
377 if (off
- rv
>= -fs
->len
&& off
- rv
<= 0)
379 fs
->pos
= fs
->len
+ off
- rv
;
385 rv
= lseek (fs
->fd
, off
, whence
);
391 /* --------------------------------------------------------------------------------------------- */
394 * Seek to the beginning of file, thrashing read cache.
396 * @param fs file structure
398 * @return 0 if success, non-zero on error
406 rv
= lseek (fs
->fd
, 0, SEEK_SET
);
412 /* --------------------------------------------------------------------------------------------- */
415 * Write bytes to file.
416 * @note thrashes read cache
418 * @param fs file structure
419 * @param buf source buffer
420 * @param size size of buffer
422 * @return number of written bytes, -1 on error
426 f_write (FBUF
* fs
, const char *buf
, size_t size
)
430 rv
= write (fs
->fd
, buf
, size
);
436 /* --------------------------------------------------------------------------------------------- */
439 * Truncate file to the current position.
440 * @note thrashes read cache
442 * @param fs file structure
444 * @return current file size on success, negative on error
452 off
= lseek (fs
->fd
, 0, SEEK_CUR
);
457 rv
= ftruncate (fs
->fd
, off
);
466 /* --------------------------------------------------------------------------------------------- */
470 * @note if this is temporary file, it is deleted
472 * @param fs file structure
473 * @return 0 on success, non-zero on error
490 /* --------------------------------------------------------------------------------------------- */
493 * Create pipe stream to process.
495 * @param cmd shell command line
496 * @param flags open mode, either O_RDONLY or O_WRONLY
498 * @return file structure
502 p_open (const char *cmd
, int flags
)
506 const char *type
= NULL
;
508 if (flags
== O_RDONLY
)
510 else if (flags
== O_WRONLY
)
520 f
= popen (cmd
, type
);
532 /* --------------------------------------------------------------------------------------------- */
537 * @param fs structure
538 * @return 0 on success, non-zero on error
548 rv
= pclose (fs
->data
);
555 /* --------------------------------------------------------------------------------------------- */
558 * Get one char (byte) from string
562 * @return TRUE on success, FALSE otherwise
566 dview_get_byte (const char *str
, int *ch
)
571 *ch
= (unsigned char) (*str
);
575 /* --------------------------------------------------------------------------------------------- */
579 * Get utf multibyte char from string
583 * @param ch_length ...
584 * @return TRUE on success, FALSE otherwise
588 dview_get_utf (const char *str
, int *ch
, int *ch_length
)
593 *ch
= g_utf8_get_char_validated (str
, -1);
597 *ch
= (unsigned char) (*str
);
604 /* Calculate UTF-8 char length */
605 next_ch
= g_utf8_next_char (str
);
606 *ch_length
= next_ch
- str
;
612 /* --------------------------------------------------------------------------------------------- */
615 dview_str_utf8_offset_to_pos (const char *text
, size_t length
)
619 if (text
== NULL
|| text
[0] == '\0')
622 if (g_utf8_validate (text
, -1, NULL
))
623 result
= g_utf8_offset_to_pointer (text
, length
) - text
;
627 char *tmpbuf
, *buffer
;
629 buffer
= tmpbuf
= g_strdup (text
);
630 while (tmpbuf
[0] != '\0')
632 uni
= g_utf8_get_char_validated (tmpbuf
, -1);
633 if ((uni
!= (gunichar
) (-1)) && (uni
!= (gunichar
) (-2)))
634 tmpbuf
= g_utf8_next_char (tmpbuf
);
641 result
= g_utf8_offset_to_pointer (tmpbuf
, length
) - tmpbuf
;
644 return MAX (length
, (size_t) result
);
646 #endif /*HAVE_CHARSET */
648 /* --------------------------------------------------------------------------------------------- */
650 /* diff parse *************************************************************** */
653 * Read decimal number from string.
655 * @param[in,out] str string to parse
656 * @param[out] n extracted number
657 * @return 0 if success, otherwise non-zero
661 scan_deci (const char **str
, int *n
)
663 const char *p
= *str
;
667 *n
= strtol (p
, &q
, 10);
668 if (errno
!= 0 || p
== q
)
674 /* --------------------------------------------------------------------------------------------- */
677 * Parse line for diff statement.
679 * @param p string to parse
680 * @param ops list of diff statements
681 * @return 0 if success, otherwise non-zero
685 scan_line (const char *p
, GArray
* ops
)
694 /* handle the following cases:
696 * NUM[,NUM]cNUM[,NUM]
698 * where NUM is a positive integer
701 if (scan_deci (&p
, &f1
) != 0 || f1
< 0)
709 if (scan_deci (&p
, &f2
) != 0 || f2
< f1
)
721 else if (cmd
!= 'c' && cmd
!= 'd')
724 if (scan_deci (&p
, &t1
) != 0 || t1
< 0)
732 if (scan_deci (&p
, &t2
) != 0 || t2
< t1
)
738 if (cmd
== 'd' && range
!= 0)
746 g_array_append_val (ops
, op
);
750 /* --------------------------------------------------------------------------------------------- */
753 * Parse diff output and extract diff statements.
755 * @param f stream to read from
756 * @param ops list of diff statements to fill
757 * @return positive number indicating number of hunks, otherwise negative
761 scan_diff (FBUF
* f
, GArray
* ops
)
766 while ((sz
= f_gets (buf
, sizeof (buf
) - 1, f
)) != 0)
768 if (isdigit (buf
[0]))
770 if (buf
[sz
- 1] != '\n')
774 if (scan_line (buf
, ops
) != 0)
780 while (buf
[sz
- 1] != '\n' && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
787 /* --------------------------------------------------------------------------------------------- */
790 * Invoke diff and extract diff statements.
792 * @param args extra arguments to be passed to diff
793 * @param extra more arguments to be passed to diff
794 * @param file1 first file to compare
795 * @param file2 second file to compare
796 * @param ops list of diff statements to fill
798 * @return positive number indicating number of hunks, otherwise negative
802 dff_execute (const char *args
, const char *extra
, const char *file1
, const char *file2
,
805 static const char *opt
=
806 " --old-group-format='%df%(f=l?:,%dl)d%dE\n'"
807 " --new-group-format='%dea%dF%(F=L?:,%dL)\n'"
808 " --changed-group-format='%df%(f=l?:,%dl)c%dF%(F=L?:,%dL)\n'"
809 " --unchanged-group-format=''";
815 char *file1_esc
, *file2_esc
;
817 /* escape potential $ to avoid shell variable substitutions in popen() */
818 file1_esc
= strutils_shell_escape (file1
);
819 file2_esc
= strutils_shell_escape (file2
);
820 cmd
= g_strdup_printf ("diff %s %s %s %s %s", args
, extra
, opt
, file1_esc
, file2_esc
);
827 f
= p_open (cmd
, O_RDONLY
);
833 rv
= scan_diff (f
, ops
);
836 if (rv
< 0 || code
== -1 || !WIFEXITED (code
) || WEXITSTATUS (code
) == 2)
842 /* --------------------------------------------------------------------------------------------- */
845 * Reparse and display file according to diff statements.
847 * @param ord DIFF_LEFT if 1nd file is displayed , DIFF_RIGHT if 2nd file is displayed.
848 * @param filename file name to display
849 * @param ops list of diff statements
850 * @param printer printf-like function to be used for displaying
851 * @param ctx printer context
853 * @return 0 if success, otherwise non-zero
857 dff_reparse (diff_place_t ord
, const char *filename
, const GArray
* ops
, DFUNC printer
, void *ctx
)
870 f
= f_open (filename
, O_RDONLY
);
886 #define T1 a[ ord^1 ][0]
887 #define T2 a[ ord^1 ][1]
888 for (i
= 0; i
< ops
->len
; i
++)
892 op
= &g_array_index (ops
, DIFFCMD
, i
);
893 n
= op
->F1
- (op
->cmd
!= add_cmd
);
895 while (line
< n
&& (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
898 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
900 while (buf
[sz
- 1] != '\n')
902 sz
= f_gets (buf
, sizeof (buf
), f
);
905 printer (ctx
, 0, 0, 0, 1, "\n");
908 printer (ctx
, 0, 0, 0, sz
, buf
);
916 if (op
->cmd
== add_cmd
)
918 n
= op
->T2
- op
->T1
+ 1;
921 printer (ctx
, DEL_CH
, 0, 0, 1, "\n");
926 if (op
->cmd
== del_cmd
)
928 n
= op
->F2
- op
->F1
+ 1;
929 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
932 printer (ctx
, ADD_CH
, line
, off
, sz
, buf
);
934 while (buf
[sz
- 1] != '\n')
936 sz
= f_gets (buf
, sizeof (buf
), f
);
939 printer (ctx
, 0, 0, 0, 1, "\n");
942 printer (ctx
, 0, 0, 0, sz
, buf
);
954 n
= op
->F2
- op
->F1
+ 1;
955 while (n
!= 0 && (sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
958 printer (ctx
, CHG_CH
, line
, off
, sz
, buf
);
960 while (buf
[sz
- 1] != '\n')
962 sz
= f_gets (buf
, sizeof (buf
), f
);
965 printer (ctx
, 0, 0, 0, 1, "\n");
968 printer (ctx
, 0, 0, 0, sz
, buf
);
977 n
= op
->T2
- op
->T1
- (op
->F2
- op
->F1
);
980 printer (ctx
, CHG_CH
, 0, 0, 1, "\n");
990 while ((sz
= f_gets (buf
, sizeof (buf
), f
)) != 0)
993 printer (ctx
, EQU_CH
, line
, off
, sz
, buf
);
995 while (buf
[sz
- 1] != '\n')
997 sz
= f_gets (buf
, sizeof (buf
), f
);
1000 printer (ctx
, 0, 0, 0, 1, "\n");
1003 printer (ctx
, 0, 0, 0, sz
, buf
);
1016 /* --------------------------------------------------------------------------------------------- */
1018 /* horizontal diff ********************************************************** */
1021 * Longest common substring.
1023 * @param s first string
1024 * @param m length of first string
1025 * @param t second string
1026 * @param n length of second string
1027 * @param ret list of offsets for longest common substrings inside each string
1028 * @param min minimum length of common substrings
1030 * @return 0 if success, nonzero otherwise
1034 lcsubstr (const char *s
, int m
, const char *t
, int n
, GArray
* ret
, int min
)
1040 if (m
< min
|| n
< min
)
1042 /* XXX early culling */
1046 Lprev
= g_try_new0 (int, n
+ 1);
1050 Lcurr
= g_try_new0 (int, n
+ 1);
1057 for (i
= 0; i
< m
; i
++)
1064 #ifdef USE_MEMSET_IN_LCS
1065 memset (Lcurr
, 0, (n
+ 1) * sizeof (*Lcurr
));
1067 for (j
= 0; j
< n
; j
++)
1069 #ifndef USE_MEMSET_IN_LCS
1081 g_array_set_size (ret
, 0);
1083 if (z
== v
&& z
>= min
)
1091 for (k
= 0; k
< ret
->len
; k
++)
1093 PAIR
*p
= (PAIR
*) g_array_index (ret
, PAIR
, k
);
1094 if ((*p
)[0] == off0
|| (*p
)[1] >= off1
)
1103 g_array_append_val (ret
, p2
);
1115 /* --------------------------------------------------------------------------------------------- */
1118 * Scan recursively for common substrings and build ranges.
1120 * @param s first string
1121 * @param t second string
1122 * @param bracket current limits for both of the strings
1123 * @param min minimum length of common substrings
1124 * @param hdiff list of horizontal diff ranges to fill
1125 * @param depth recursion depth
1127 * @return 0 if success, nonzero otherwise
1131 hdiff_multi (const char *s
, const char *t
, const BRACKET bracket
, int min
, GArray
* hdiff
,
1142 ret
= g_array_new (FALSE
, TRUE
, sizeof (PAIR
));
1146 len
= lcsubstr (s
+ bracket
[DIFF_LEFT
].off
, bracket
[DIFF_LEFT
].len
,
1147 t
+ bracket
[DIFF_RIGHT
].off
, bracket
[DIFF_RIGHT
].len
, ret
, min
);
1151 const PAIR
*data
= (const PAIR
*) &g_array_index (ret
, PAIR
, 0);
1154 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1155 b
[DIFF_LEFT
].len
= (*data
)[0];
1156 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1157 b
[DIFF_RIGHT
].len
= (*data
)[1];
1158 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1161 for (k
= 0; k
< ret
->len
- 1; k
++)
1163 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1164 data2
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
+ 1);
1165 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1166 b
[DIFF_LEFT
].len
= (*data2
)[0] - (*data
)[0] - len
;
1167 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1168 b
[DIFF_RIGHT
].len
= (*data2
)[1] - (*data
)[1] - len
;
1169 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1172 data
= (const PAIR
*) &g_array_index (ret
, PAIR
, k
);
1173 b
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
+ (*data
)[0] + len
;
1174 b
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
- (*data
)[0] - len
;
1175 b
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
+ (*data
)[1] + len
;
1176 b
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
- (*data
)[1] - len
;
1177 if (!hdiff_multi (s
, t
, b
, min
, hdiff
, depth
))
1180 g_array_free (ret
, TRUE
);
1185 p
[DIFF_LEFT
].off
= bracket
[DIFF_LEFT
].off
;
1186 p
[DIFF_LEFT
].len
= bracket
[DIFF_LEFT
].len
;
1187 p
[DIFF_RIGHT
].off
= bracket
[DIFF_RIGHT
].off
;
1188 p
[DIFF_RIGHT
].len
= bracket
[DIFF_RIGHT
].len
;
1189 g_array_append_val (hdiff
, p
);
1194 /* --------------------------------------------------------------------------------------------- */
1197 * Build list of horizontal diff ranges.
1199 * @param s first string
1200 * @param m length of first string
1201 * @param t second string
1202 * @param n length of second string
1203 * @param min minimum length of common substrings
1204 * @param hdiff list of horizontal diff ranges to fill
1205 * @param depth recursion depth
1207 * @return 0 if success, nonzero otherwise
1211 hdiff_scan (const char *s
, int m
, const char *t
, int n
, int min
, GArray
* hdiff
, unsigned int depth
)
1216 /* dumbscan (single horizontal diff) -- does not compress whitespace */
1217 for (i
= 0; i
< m
&& i
< n
&& s
[i
] == t
[i
]; i
++)
1219 for (; m
> i
&& n
> i
&& s
[m
- 1] == t
[n
- 1]; m
--, n
--)
1222 b
[DIFF_LEFT
].off
= i
;
1223 b
[DIFF_LEFT
].len
= m
- i
;
1224 b
[DIFF_RIGHT
].off
= i
;
1225 b
[DIFF_RIGHT
].len
= n
- i
;
1227 /* smartscan (multiple horizontal diff) */
1228 return hdiff_multi (s
, t
, b
, min
, hdiff
, depth
);
1231 /* --------------------------------------------------------------------------------------------- */
1233 /* read line **************************************************************** */
1236 * Check if character is inside horizontal diff limits.
1238 * @param k rank of character inside line
1239 * @param hdiff horizontal diff structure
1240 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1242 * @return TRUE if inside hdiff limits, FALSE otherwise
1246 is_inside (int k
, GArray
* hdiff
, diff_place_t ord
)
1251 for (i
= 0; i
< hdiff
->len
; i
++)
1255 b
= &g_array_index (hdiff
, BRACKET
, i
);
1256 start
= (*b
)[ord
].off
;
1257 end
= start
+ (*b
)[ord
].len
;
1258 if (k
>= start
&& k
< end
)
1264 /* --------------------------------------------------------------------------------------------- */
1267 * Copy 'src' to 'dst' expanding tabs.
1268 * @note The procedure returns when all bytes are consumed from 'src'
1270 * @param dst destination buffer
1271 * @param src source buffer
1272 * @param srcsize size of src buffer
1273 * @param base virtual base of this string, needed to calculate tabs
1274 * @param ts tab size
1276 * @return new virtual base
1280 cvt_cpy (char *dst
, const char *src
, size_t srcsize
, int base
, int ts
)
1284 for (i
= 0; srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1291 j
= TAB_SKIP (ts
, i
+ base
);
1301 /* --------------------------------------------------------------------------------------------- */
1304 * Copy 'src' to 'dst' expanding tabs.
1306 * @param dst destination buffer
1307 * @param dstsize size of dst buffer
1308 * @param[in,out] _src source buffer
1309 * @param srcsize size of src buffer
1310 * @param base virtual base of this string, needed to calculate tabs
1311 * @param ts tab size
1313 * @return new virtual base
1315 * @note The procedure returns when all bytes are consumed from 'src'
1316 * or 'dstsize' bytes are written to 'dst'
1317 * @note Upon return, 'src' points to the first unwritten character in source
1321 cvt_ncpy (char *dst
, int dstsize
, const char **_src
, size_t srcsize
, int base
, int ts
)
1324 const char *src
= *_src
;
1326 for (i
= 0; i
< dstsize
&& srcsize
!= 0; i
++, src
++, dst
++, srcsize
--)
1333 j
= TAB_SKIP (ts
, i
+ base
);
1334 if (j
> dstsize
- i
)
1346 /* --------------------------------------------------------------------------------------------- */
1349 * Read line from memory, converting tabs to spaces and padding with spaces.
1351 * @param src buffer to read from
1352 * @param srcsize size of src buffer
1353 * @param dst buffer to read to
1354 * @param dstsize size of dst buffer, excluding trailing null
1355 * @param skip number of characters to skip
1356 * @param ts tab size
1357 * @param show_cr show trailing carriage return as ^M
1359 * @return negative on error, otherwise number of bytes except padding
1363 cvt_mget (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
,
1374 for (i
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, src
++, srcsize
--)
1380 j
= TAB_SKIP (ts
, i
+ base
);
1386 else if (dstsize
!= 0)
1393 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1395 if (skip
== 0 && show_cr
)
1417 (void) dview_get_utf (src
, &ch
, &ch_length
);
1420 skip
+= ch_length
- 1;
1433 while (dstsize
!= 0)
1442 /* --------------------------------------------------------------------------------------------- */
1445 * Read line from memory and build attribute array.
1447 * @param src buffer to read from
1448 * @param srcsize size of src buffer
1449 * @param dst buffer to read to
1450 * @param dstsize size of dst buffer, excluding trailing null
1451 * @param skip number of characters to skip
1452 * @param ts tab size
1453 * @param show_cr show trailing carriage return as ^M
1454 * @param hdiff horizontal diff structure
1455 * @param ord DIFF_LEFT if reading from first file, DIFF_RIGHT if reading from 2nd file
1456 * @param att buffer of attributes
1458 * @return negative on error, otherwise number of bytes except padding
1462 cvt_mgeta (const char *src
, size_t srcsize
, char *dst
, int dstsize
, int skip
, int ts
,
1463 gboolean show_cr
, GArray
* hdiff
, diff_place_t ord
, char *att
)
1473 for (i
= 0, k
= 0; dstsize
!= 0 && srcsize
!= 0 && *src
!= '\n'; i
++, k
++, src
++, srcsize
--)
1479 j
= TAB_SKIP (ts
, i
+ base
);
1485 else if (dstsize
!= 0)
1488 *att
++ = is_inside (k
, hdiff
, ord
);
1493 else if (src
[0] == '\r' && (srcsize
== 1 || src
[1] == '\n'))
1495 if (skip
== 0 && show_cr
)
1500 *att
++ = is_inside (k
, hdiff
, ord
);
1502 *att
++ = is_inside (k
, hdiff
, ord
);
1508 *att
++ = is_inside (k
, hdiff
, ord
);
1520 (void) dview_get_utf (src
, &ch
, &ch_length
);
1522 skip
+= ch_length
- 1;
1530 *att
++ = is_inside (k
, hdiff
, ord
);
1536 while (dstsize
!= 0)
1546 /* --------------------------------------------------------------------------------------------- */
1549 * Read line from file, converting tabs to spaces and padding with spaces.
1551 * @param f file stream to read from
1552 * @param off offset of line inside file
1553 * @param dst buffer to read to
1554 * @param dstsize size of dst buffer, excluding trailing null
1555 * @param skip number of characters to skip
1556 * @param ts tab size
1557 * @param show_cr show trailing carriage return as ^M
1559 * @return negative on error, otherwise number of bytes except padding
1563 cvt_fget (FBUF
* f
, off_t off
, char *dst
, size_t dstsize
, int skip
, int ts
, gboolean show_cr
)
1566 int old_base
= base
;
1567 size_t amount
= dstsize
;
1568 size_t useful
, offset
;
1572 const char *q
= NULL
;
1573 char tmp
[BUFSIZ
]; /* XXX capacity must be >= MAX{dstsize + 1, amount} */
1574 char cvt
[BUFSIZ
]; /* XXX capacity must be >= MAX_TAB_WIDTH * amount */
1576 if (sizeof (tmp
) < amount
|| sizeof (tmp
) <= dstsize
|| sizeof (cvt
) < 8 * amount
)
1578 /* abnormal, but avoid buffer overflow */
1579 memset (dst
, ' ', dstsize
);
1580 dst
[dstsize
] = '\0';
1584 f_seek (f
, off
, SEEK_SET
);
1589 sz
= f_gets (tmp
, amount
, f
);
1593 base
= cvt_cpy (cvt
, tmp
, sz
, old_base
, ts
);
1594 if (cvt
[base
- old_base
- 1] == '\n')
1596 q
= &cvt
[base
- old_base
- 1];
1597 base
= old_base
+ q
- cvt
+ 1;
1604 memset (dst
, ' ', dstsize
);
1605 dst
[dstsize
] = '\0';
1609 useful
= base
- skip
;
1610 offset
= skip
- old_base
;
1612 if (useful
<= dstsize
)
1615 memmove (dst
, cvt
+ offset
, useful
);
1619 sz
= f_gets (tmp
, dstsize
- useful
+ 1, f
);
1622 const char *ptr
= tmp
;
1624 useful
+= cvt_ncpy (dst
+ useful
, dstsize
- useful
, &ptr
, sz
, base
, ts
) - base
;
1633 memmove (dst
, cvt
+ offset
, dstsize
);
1635 lastch
= cvt
[offset
+ dstsize
];
1639 for (i
= 0; i
< sz
&& dst
[i
] != '\n'; i
++)
1641 if (dst
[i
] == '\r' && dst
[i
+ 1] == '\n')
1645 if (i
+ 1 < dstsize
)
1659 for (; i
< dstsize
; i
++)
1665 /* --------------------------------------------------------------------------------------------- */
1666 /* diff printers et al ****************************************************** */
1669 cc_free_elt (void *elt
)
1677 /* --------------------------------------------------------------------------------------------- */
1680 printer (void *ctx
, int ch
, int line
, off_t off
, size_t sz
, const char *str
)
1682 GArray
*a
= ((PRINTER_CTX
*) ctx
)->a
;
1683 DSRC dsrc
= ((PRINTER_CTX
*) ctx
)->dsrc
;
1693 if (dsrc
== DATA_SRC_MEM
&& line
!= 0)
1695 if (sz
!= 0 && str
[sz
- 1] == '\n')
1698 p
.p
= g_strndup (str
, sz
);
1701 g_array_append_val (a
, p
);
1703 else if (dsrc
== DATA_SRC_MEM
)
1707 p
= &g_array_index (a
, DIFFLN
, a
->len
- 1);
1708 if (sz
!= 0 && str
[sz
- 1] == '\n')
1715 new_size
= p
->u
.len
+ sz
;
1716 q
= g_realloc (p
->p
, new_size
);
1717 memcpy (q
+ p
->u
.len
, str
, sz
);
1722 if (dsrc
== DATA_SRC_TMP
&& (line
!= 0 || ch
== 0))
1724 FBUF
*f
= ((PRINTER_CTX
*) ctx
)->f
;
1725 f_write (f
, str
, sz
);
1730 /* --------------------------------------------------------------------------------------------- */
1733 redo_diff (WDiff
* dview
)
1735 FBUF
*const *f
= dview
->f
;
1743 if (dview
->opt
.quality
== 2)
1744 strcat (extra
, " -d");
1745 if (dview
->opt
.quality
== 1)
1746 strcat (extra
, " --speed-large-files");
1747 if (dview
->opt
.strip_trailing_cr
)
1748 strcat (extra
, " --strip-trailing-cr");
1749 if (dview
->opt
.ignore_tab_expansion
)
1750 strcat (extra
, " -E");
1751 if (dview
->opt
.ignore_space_change
)
1752 strcat (extra
, " -b");
1753 if (dview
->opt
.ignore_all_space
)
1754 strcat (extra
, " -w");
1755 if (dview
->opt
.ignore_case
)
1756 strcat (extra
, " -i");
1758 if (dview
->dsrc
!= DATA_SRC_MEM
)
1760 f_reset (f
[DIFF_LEFT
]);
1761 f_reset (f
[DIFF_RIGHT
]);
1764 ops
= g_array_new (FALSE
, FALSE
, sizeof (DIFFCMD
));
1765 ndiff
= dff_execute (dview
->args
, extra
, dview
->file
[DIFF_LEFT
], dview
->file
[DIFF_RIGHT
], ops
);
1769 g_array_free (ops
, TRUE
);
1773 ctx
.dsrc
= dview
->dsrc
;
1776 ctx
.a
= dview
->a
[DIFF_LEFT
];
1777 ctx
.f
= f
[DIFF_LEFT
];
1778 rv
|= dff_reparse (DIFF_LEFT
, dview
->file
[DIFF_LEFT
], ops
, printer
, &ctx
);
1780 ctx
.a
= dview
->a
[DIFF_RIGHT
];
1781 ctx
.f
= f
[DIFF_RIGHT
];
1782 rv
|= dff_reparse (DIFF_RIGHT
, dview
->file
[DIFF_RIGHT
], ops
, printer
, &ctx
);
1785 g_array_free (ops
, TRUE
);
1787 if (rv
!= 0 || dview
->a
[DIFF_LEFT
]->len
!= dview
->a
[DIFF_RIGHT
]->len
)
1790 if (dview
->dsrc
== DATA_SRC_TMP
)
1792 f_trunc (f
[DIFF_LEFT
]);
1793 f_trunc (f
[DIFF_RIGHT
]);
1796 if (dview
->dsrc
== DATA_SRC_MEM
&& HDIFF_ENABLE
)
1798 dview
->hdiff
= g_ptr_array_new ();
1799 if (dview
->hdiff
!= NULL
)
1803 for (i
= 0; i
< dview
->a
[DIFF_LEFT
]->len
; i
++)
1809 p
= &g_array_index (dview
->a
[DIFF_LEFT
], DIFFLN
, i
);
1810 q
= &g_array_index (dview
->a
[DIFF_RIGHT
], DIFFLN
, i
);
1811 if (p
->line
&& q
->line
&& p
->ch
== CHG_CH
)
1813 h
= g_array_new (FALSE
, FALSE
, sizeof (BRACKET
));
1819 hdiff_scan (p
->p
, p
->u
.len
, q
->p
, q
->u
.len
, HDIFF_MINCTX
, h
,
1823 g_array_free (h
, TRUE
);
1828 g_ptr_array_add (dview
->hdiff
, h
);
1835 /* --------------------------------------------------------------------------------------------- */
1838 destroy_hdiff (WDiff
* dview
)
1840 if (dview
->hdiff
!= NULL
)
1845 len
= dview
->a
[DIFF_LEFT
]->len
;
1847 for (i
= 0; i
< len
; i
++)
1851 h
= (GArray
*) g_ptr_array_index (dview
->hdiff
, i
);
1853 g_array_free (h
, TRUE
);
1855 g_ptr_array_free (dview
->hdiff
, TRUE
);
1856 dview
->hdiff
= NULL
;
1859 mc_search_free (dview
->search
.handle
);
1860 dview
->search
.handle
= NULL
;
1861 MC_PTR_FREE (dview
->search
.last_string
);
1864 /* --------------------------------------------------------------------------------------------- */
1865 /* stuff ******************************************************************** */
1868 get_digits (unsigned int n
)
1877 /* --------------------------------------------------------------------------------------------- */
1880 get_line_numbers (const GArray
* a
, size_t pos
, int *linenum
, int *lineofs
)
1892 p
= &g_array_index (a
, DIFFLN
, pos
);
1898 for (n
= pos
; n
> 0; n
--)
1904 *lineofs
= pos
- n
+ 1;
1912 /* --------------------------------------------------------------------------------------------- */
1915 calc_nwidth (const GArray
* const *a
)
1920 get_line_numbers (a
[DIFF_LEFT
], a
[DIFF_LEFT
]->len
- 1, &l1
, &o1
);
1921 get_line_numbers (a
[DIFF_RIGHT
], a
[DIFF_RIGHT
]->len
- 1, &l2
, &o2
);
1924 return get_digits (l1
);
1927 /* --------------------------------------------------------------------------------------------- */
1930 find_prev_hunk (const GArray
* a
, int pos
)
1933 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1935 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1937 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1939 if (pos
> 0 && (size_t) pos
< a
->len
)
1942 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
== EQU_CH
)
1944 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
- 1))->ch
!= EQU_CH
)
1951 /* --------------------------------------------------------------------------------------------- */
1954 find_next_hunk (const GArray
* a
, size_t pos
)
1956 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
!= EQU_CH
)
1958 while (pos
< a
->len
&& ((DIFFLN
*) & g_array_index (a
, DIFFLN
, pos
))->ch
== EQU_CH
)
1963 /* --------------------------------------------------------------------------------------------- */
1965 * Find start and end lines of the current hunk.
1967 * @param dview WDiff widget
1968 * @return boolean and
1969 * start_line1 first line of current hunk (file[0])
1970 * end_line1 last line of current hunk (file[0])
1971 * start_line1 first line of current hunk (file[0])
1972 * end_line1 last line of current hunk (file[0])
1976 get_current_hunk (WDiff
* dview
, int *start_line1
, int *end_line1
, int *start_line2
, int *end_line2
)
1978 const GArray
*a0
= dview
->a
[DIFF_LEFT
];
1979 const GArray
*a1
= dview
->a
[DIFF_RIGHT
];
1989 pos
= dview
->skip_rows
;
1990 ch
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
;
2007 while (pos
> 0 && ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2011 *start_line1
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
+ 1;
2012 *start_line2
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
+ 1;
2014 pos
= dview
->skip_rows
;
2015 while (pos
< a0
->len
&& ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->ch
!= EQU_CH
)
2019 l0
= ((DIFFLN
*) & g_array_index (a0
, DIFFLN
, pos
))->line
;
2020 l1
= ((DIFFLN
*) & g_array_index (a1
, DIFFLN
, pos
))->line
;
2022 *end_line1
= MAX (*start_line1
, l0
);
2024 *end_line2
= MAX (*start_line2
, l1
);
2031 /* --------------------------------------------------------------------------------------------- */
2033 * Remove hunk from file.
2035 * @param dview WDiff widget
2036 * @param merge_file file stream for writing data
2037 * @param from1 first line of hunk
2038 * @param to1 last line of hunk
2039 * @param merge_direction in what direction files should be merged
2043 dview_remove_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
,
2044 action_direction_t merge_direction
)
2050 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2051 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2053 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2056 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2059 fputs (buf
, merge_file
);
2061 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2065 fputs (buf
, merge_file
);
2070 /* --------------------------------------------------------------------------------------------- */
2074 * @param dview WDiff widget
2075 * @param merge_file file stream for writing data
2076 * @param from1 first line of source hunk
2077 * @param from2 first line of destination hunk
2078 * @param to1 last line of source hunk
2079 * @param merge_direction in what direction files should be merged
2083 dview_add_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int from2
, int to2
,
2084 action_direction_t merge_direction
)
2091 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2093 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2094 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2098 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2099 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2103 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line
< from1
- 1)
2106 fputs (buf
, merge_file
);
2109 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line
<= to2
)
2113 fputs (buf
, merge_file
);
2115 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2116 fputs (buf
, merge_file
);
2122 /* --------------------------------------------------------------------------------------------- */
2124 * Replace hunk in file.
2126 * @param dview WDiff widget
2127 * @param merge_file file stream for writing data
2128 * @param from1 first line of source hunk
2129 * @param to1 last line of source hunk
2130 * @param from2 first line of destination hunk
2131 * @param to2 last line of destination hunk
2132 * @param merge_direction in what direction files should be merged
2136 dview_replace_hunk (WDiff
* dview
, FILE * merge_file
, int from1
, int to1
, int from2
, int to2
,
2137 action_direction_t merge_direction
)
2139 int line1
= 0, line2
= 0;
2144 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2146 f0
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2147 f1
= fopen (dview
->file
[DIFF_LEFT
], "r");
2151 f0
= fopen (dview
->file
[DIFF_LEFT
], "r");
2152 f1
= fopen (dview
->file
[DIFF_RIGHT
], "r");
2155 while (fgets (buf
, sizeof (buf
), f0
) != NULL
&& line1
< from1
- 1)
2158 fputs (buf
, merge_file
);
2160 while (fgets (buf
, sizeof (buf
), f1
) != NULL
&& line2
<= to2
)
2164 fputs (buf
, merge_file
);
2166 while (fgets (buf
, sizeof (buf
), f0
) != NULL
)
2170 fputs (buf
, merge_file
);
2176 /* --------------------------------------------------------------------------------------------- */
2180 * @param dview WDiff widget
2181 * @param merge_direction in what direction files should be merged
2185 do_merge_hunk (WDiff
* dview
, action_direction_t merge_direction
)
2187 int from1
, to1
, from2
, to2
;
2189 diff_place_t n_merge
= (merge_direction
== FROM_RIGHT_TO_LEFT
) ? DIFF_RIGHT
: DIFF_LEFT
;
2191 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2192 hunk
= get_current_hunk (dview
, &from2
, &to2
, &from1
, &to1
);
2194 hunk
= get_current_hunk (dview
, &from1
, &to1
, &from2
, &to2
);
2200 vfs_path_t
*merge_file_name_vpath
= NULL
;
2202 if (!dview
->merged
[n_merge
])
2204 dview
->merged
[n_merge
] = mc_util_make_backup_if_possible (dview
->file
[n_merge
], "~~~");
2205 if (!dview
->merged
[n_merge
])
2207 message (D_ERROR
, MSG_ERROR
,
2208 _("Cannot create backup file\n%s%s\n%s"),
2209 dview
->file
[n_merge
], "~~~", unix_error_string (errno
));
2214 merge_file_fd
= mc_mkstemps (&merge_file_name_vpath
, "mcmerge", NULL
);
2215 if (merge_file_fd
== -1)
2217 message (D_ERROR
, MSG_ERROR
, _("Cannot create temporary merge file\n%s"),
2218 unix_error_string (errno
));
2222 merge_file
= fdopen (merge_file_fd
, "w");
2227 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2228 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_RIGHT_TO_LEFT
);
2230 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_LEFT_TO_RIGHT
);
2233 if (merge_direction
== FROM_RIGHT_TO_LEFT
)
2234 dview_remove_hunk (dview
, merge_file
, from1
, to1
, FROM_RIGHT_TO_LEFT
);
2236 dview_add_hunk (dview
, merge_file
, from1
, from2
, to2
, FROM_LEFT_TO_RIGHT
);
2239 dview_replace_hunk (dview
, merge_file
, from1
, to1
, from2
, to2
, merge_direction
);
2244 fflush (merge_file
);
2245 fclose (merge_file
);
2249 res
= rewrite_backup_content (merge_file_name_vpath
, dview
->file
[n_merge
]);
2252 mc_unlink (merge_file_name_vpath
);
2253 vfs_path_free (merge_file_name_vpath
);
2257 /* --------------------------------------------------------------------------------------------- */
2258 /* view routines and callbacks ********************************************** */
2261 dview_compute_split (WDiff
* dview
, int i
)
2264 if (dview
->bias
< 2 - dview
->half1
)
2265 dview
->bias
= 2 - dview
->half1
;
2266 if (dview
->bias
> dview
->half2
- 2)
2267 dview
->bias
= dview
->half2
- 2;
2270 /* --------------------------------------------------------------------------------------------- */
2273 dview_compute_areas (WDiff
* dview
)
2275 Widget
*w
= WIDGET (dview
);
2277 dview
->height
= w
->lines
- 1;
2278 dview
->half1
= w
->cols
/ 2;
2279 dview
->half2
= w
->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 quick_widget_t quick_widgets
[] = {
2364 QUICK_START_GROUPBOX (N_("Diff algorithm")),
2365 QUICK_RADIO (3, (const char **) quality_str
, (int *) &dview
->opt
.quality
, NULL
),
2366 QUICK_STOP_GROUPBOX
,
2367 QUICK_START_GROUPBOX (N_("Diff extra options")),
2368 QUICK_CHECKBOX (N_("&Ignore case"), &dview
->opt
.ignore_case
, NULL
),
2369 QUICK_CHECKBOX (N_("Ignore tab &expansion"), &dview
->opt
.ignore_tab_expansion
, NULL
),
2370 QUICK_CHECKBOX (N_("Ignore &space change"), &dview
->opt
.ignore_space_change
, NULL
),
2371 QUICK_CHECKBOX (N_("Ignore all &whitespace"), &dview
->opt
.ignore_all_space
, NULL
),
2372 QUICK_CHECKBOX (N_("Strip &trailing carriage return"), &dview
->opt
.strip_trailing_cr
,
2374 QUICK_STOP_GROUPBOX
,
2375 QUICK_BUTTONS_OK_CANCEL
,
2380 quick_dialog_t qdlg
= {
2382 N_("Diff Options"), "[Diff Options]",
2383 quick_widgets
, NULL
, NULL
2386 if (quick_dialog (&qdlg
) != B_CANCEL
)
2387 dview_reread (dview
);
2390 /* --------------------------------------------------------------------------------------------- */
2393 dview_init (WDiff
* dview
, const char *args
, const char *file1
, const char *file2
,
2394 const char *label1
, const char *label2
, DSRC dsrc
)
2397 FBUF
*f
[DIFF_COUNT
];
2399 f
[DIFF_LEFT
] = NULL
;
2400 f
[DIFF_RIGHT
] = NULL
;
2402 if (dsrc
== DATA_SRC_TMP
)
2404 f
[DIFF_LEFT
] = f_temp ();
2405 if (f
[DIFF_LEFT
] == NULL
)
2408 f
[DIFF_RIGHT
] = f_temp ();
2409 if (f
[DIFF_RIGHT
] == NULL
)
2411 f_close (f
[DIFF_LEFT
]);
2415 else if (dsrc
== DATA_SRC_ORG
)
2417 f
[DIFF_LEFT
] = f_open (file1
, O_RDONLY
);
2418 if (f
[DIFF_LEFT
] == NULL
)
2421 f
[DIFF_RIGHT
] = f_open (file2
, O_RDONLY
);
2422 if (f
[DIFF_RIGHT
] == NULL
)
2424 f_close (f
[DIFF_LEFT
]);
2430 dview
->file
[DIFF_LEFT
] = file1
;
2431 dview
->file
[DIFF_RIGHT
] = file2
;
2432 dview
->label
[DIFF_LEFT
] = g_strdup (label1
);
2433 dview
->label
[DIFF_RIGHT
] = g_strdup (label2
);
2434 dview
->f
[DIFF_LEFT
] = f
[0];
2435 dview
->f
[DIFF_RIGHT
] = f
[1];
2436 dview
->merged
[DIFF_LEFT
] = FALSE
;
2437 dview
->merged
[DIFF_RIGHT
] = FALSE
;
2438 dview
->hdiff
= NULL
;
2441 dview
->converter
= str_cnv_from_term
;
2442 dview_set_codeset (dview
);
2444 dview
->a
[DIFF_LEFT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2445 dview
->a
[DIFF_RIGHT
] = g_array_new (FALSE
, FALSE
, sizeof (DIFFLN
));
2447 ndiff
= redo_diff (dview
);
2450 /* goto MSG_DESTROY stage: dview_fini() */
2451 f_close (f
[DIFF_LEFT
]);
2452 f_close (f
[DIFF_RIGHT
]);
2456 dview
->ndiff
= ndiff
;
2458 dview
->view_quit
= FALSE
;
2461 dview
->new_frame
= TRUE
;
2462 dview
->skip_rows
= 0;
2463 dview
->skip_cols
= 0;
2464 dview
->display_symbols
= 0;
2465 dview
->display_numbers
= 0;
2466 dview
->show_cr
= TRUE
;
2467 dview
->tab_size
= 8;
2468 dview
->ord
= DIFF_LEFT
;
2469 dview
->full
= FALSE
;
2471 dview
->search
.handle
= NULL
;
2472 dview
->search
.last_string
= NULL
;
2473 dview
->search
.last_found_line
= -1;
2474 dview
->search
.last_accessed_num_line
= -1;
2476 dview
->opt
.quality
= 0;
2477 dview
->opt
.strip_trailing_cr
= 0;
2478 dview
->opt
.ignore_tab_expansion
= 0;
2479 dview
->opt
.ignore_space_change
= 0;
2480 dview
->opt
.ignore_all_space
= 0;
2481 dview
->opt
.ignore_case
= 0;
2483 dview_compute_areas (dview
);
2488 /* --------------------------------------------------------------------------------------------- */
2491 dview_fini (WDiff
* dview
)
2493 if (dview
->dsrc
!= DATA_SRC_MEM
)
2495 f_close (dview
->f
[DIFF_RIGHT
]);
2496 f_close (dview
->f
[DIFF_LEFT
]);
2500 if (dview
->converter
!= str_cnv_from_term
)
2501 str_close_conv (dview
->converter
);
2504 destroy_hdiff (dview
);
2505 if (dview
->a
[DIFF_LEFT
] != NULL
)
2507 g_array_foreach (dview
->a
[DIFF_LEFT
], DIFFLN
, cc_free_elt
);
2508 g_array_free (dview
->a
[DIFF_LEFT
], TRUE
);
2509 dview
->a
[DIFF_LEFT
] = NULL
;
2511 if (dview
->a
[DIFF_RIGHT
] != NULL
)
2513 g_array_foreach (dview
->a
[DIFF_RIGHT
], DIFFLN
, cc_free_elt
);
2514 g_array_free (dview
->a
[DIFF_RIGHT
], TRUE
);
2515 dview
->a
[DIFF_RIGHT
] = NULL
;
2518 g_free (dview
->label
[DIFF_LEFT
]);
2519 g_free (dview
->label
[DIFF_RIGHT
]);
2522 /* --------------------------------------------------------------------------------------------- */
2525 dview_display_file (const WDiff
* dview
, diff_place_t ord
, int r
, int c
, int height
, int width
)
2530 FBUF
*f
= dview
->f
[ord
];
2531 int skip
= dview
->skip_cols
;
2532 int display_symbols
= dview
->display_symbols
;
2533 int display_numbers
= dview
->display_numbers
;
2534 gboolean show_cr
= dview
->show_cr
;
2537 int nwidth
= display_numbers
;
2540 xwidth
= display_symbols
+ display_numbers
;
2541 if (dview
->tab_size
> 0 && dview
->tab_size
< 9)
2542 tab_size
= dview
->tab_size
;
2546 if (xwidth
> width
&& display_symbols
)
2549 display_symbols
= 0;
2551 if (xwidth
> width
&& display_numbers
)
2554 display_numbers
= width
;
2564 if ((int) sizeof (buf
) <= width
|| (int) sizeof (buf
) <= nwidth
)
2566 /* abnormal, but avoid buffer overflow */
2570 for (i
= dview
->skip_rows
, j
= 0; i
< dview
->a
[ord
]->len
&& j
< height
; j
++, i
++)
2572 int ch
, next_ch
= 0, col
;
2575 p
= (DIFFLN
*) & g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2577 tty_setcolor (NORMAL_COLOR
);
2578 if (display_symbols
)
2580 tty_gotoyx (r
+ j
, c
- 2);
2581 tty_print_char (ch
);
2585 if (display_numbers
)
2587 tty_gotoyx (r
+ j
, c
- xwidth
);
2588 g_snprintf (buf
, display_numbers
+ 1, "%*d", nwidth
, p
->line
);
2589 tty_print_string (str_fit_to_term (buf
, nwidth
, J_LEFT_FIT
));
2592 tty_setcolor (DFF_ADD_COLOR
);
2594 tty_setcolor (DFF_CHG_COLOR
);
2597 if (i
== (size_t) dview
->search
.last_found_line
)
2598 tty_setcolor (MARKED_SELECTED_COLOR
);
2599 else if (dview
->hdiff
!= NULL
&& g_ptr_array_index (dview
->hdiff
, i
) != NULL
)
2605 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
++)
2624 ch_res
= dview_get_utf (buf
+ cnt
, &next_ch
, &ch_length
);
2626 cnt
+= ch_length
- 1;
2627 if (!g_unichar_isprint (next_ch
))
2632 ch_res
= dview_get_byte (buf
+ cnt
, &next_ch
);
2636 tty_setcolor (att
[cnt
] ? DFF_CHH_COLOR
: DFF_CHG_COLOR
);
2638 if (mc_global
.utf8_display
)
2643 convert_from_8bit_to_utf_c ((unsigned char) next_ch
,
2647 else if (dview
->utf8
)
2648 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2650 next_ch
= convert_to_display_c (next_ch
);
2652 tty_print_anychar (next_ch
);
2660 tty_setcolor (DFF_CHH_COLOR
);
2664 k
= dview_str_utf8_offset_to_pos (p
->p
, width
);
2668 cvt_mget (p
->p
, p
->u
.len
, buf
, k
, skip
, tab_size
, show_cr
);
2671 cvt_fget (f
, p
->u
.off
, buf
, width
, skip
, tab_size
, show_cr
);
2675 if (display_numbers
)
2677 tty_gotoyx (r
+ j
, c
- xwidth
);
2678 memset (buf
, ' ', display_numbers
);
2679 buf
[display_numbers
] = '\0';
2680 tty_print_string (buf
);
2683 tty_setcolor (DFF_DEL_COLOR
);
2685 tty_setcolor (DFF_CHD_COLOR
);
2686 memset (buf
, ' ', width
);
2689 tty_gotoyx (r
+ j
, c
);
2690 /* tty_print_nstring (buf, width); */
2692 for (cnt
= 0; cnt
< strlen (buf
) && col
< width
; cnt
++)
2701 ch_res
= dview_get_utf (buf
+ cnt
, &next_ch
, &ch_length
);
2703 cnt
+= ch_length
- 1;
2704 if (!g_unichar_isprint (next_ch
))
2709 ch_res
= dview_get_byte (buf
+ cnt
, &next_ch
);
2714 if (mc_global
.utf8_display
)
2719 convert_from_8bit_to_utf_c ((unsigned char) next_ch
, dview
->converter
);
2722 else if (dview
->utf8
)
2723 next_ch
= convert_from_utf_to_current_c (next_ch
, dview
->converter
);
2725 next_ch
= convert_to_display_c (next_ch
);
2728 tty_print_anychar (next_ch
);
2733 tty_setcolor (NORMAL_COLOR
);
2735 if (width
< xwidth
- 1)
2737 memset (buf
, ' ', k
);
2739 for (; j
< height
; j
++)
2743 tty_gotoyx (r
+ j
, c
- xwidth
);
2744 /* tty_print_nstring (buf, xwidth - 1); */
2745 tty_print_string (str_fit_to_term (buf
, xwidth
- 1, J_LEFT_FIT
));
2747 tty_gotoyx (r
+ j
, c
);
2748 /* tty_print_nstring (buf, width); */
2749 tty_print_string (str_fit_to_term (buf
, width
, J_LEFT_FIT
));
2755 /* --------------------------------------------------------------------------------------------- */
2758 dview_status (const WDiff
* dview
, diff_place_t ord
, int width
, int c
)
2762 int linenum
, lineofs
;
2766 tty_setcolor (STATUSBAR_COLOR
);
2769 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2771 filename_width
= width
- 24;
2772 if (filename_width
< 8)
2775 vpath
= vfs_path_from_str (dview
->label
[ord
]);
2776 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
| VPF_STRIP_PASSWORD
);
2777 vfs_path_free (vpath
);
2778 buf
= str_term_trim (path
, filename_width
);
2779 if (ord
== DIFF_LEFT
)
2780 tty_printf ("%s%-*s %6d+%-4d Col %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2781 buf
, linenum
, lineofs
, dview
->skip_cols
);
2783 tty_printf ("%s%-*s %6d+%-4d Dif %-4d ", dview
->merged
[ord
] ? "* " : " ", filename_width
,
2784 buf
, linenum
, lineofs
, dview
->ndiff
);
2788 /* --------------------------------------------------------------------------------------------- */
2791 dview_redo (WDiff
* dview
)
2793 if (dview
->display_numbers
)
2797 old
= dview
->display_numbers
;
2798 dview
->display_numbers
= calc_nwidth ((const GArray
* const *) dview
->a
);
2799 dview
->new_frame
= (old
!= dview
->display_numbers
);
2801 dview_reread (dview
);
2804 /* --------------------------------------------------------------------------------------------- */
2807 dview_update (WDiff
* dview
)
2809 int height
= dview
->height
;
2814 last
= dview
->a
[DIFF_LEFT
]->len
- 1;
2816 if (dview
->skip_rows
> last
)
2817 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= last
;
2818 if (dview
->skip_rows
< 0)
2819 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
2820 if (dview
->skip_cols
< 0)
2821 dview
->skip_cols
= 0;
2826 width1
= dview
->half1
+ dview
->bias
;
2827 width2
= dview
->half2
- dview
->bias
;
2834 if (dview
->new_frame
)
2838 tty_setcolor (NORMAL_COLOR
);
2839 xwidth
= dview
->display_symbols
+ dview
->display_numbers
;
2841 tty_draw_box (1, 0, height
, width1
, FALSE
);
2843 tty_draw_box (1, width1
, height
, width2
, FALSE
);
2848 if (xwidth
< width1
- 1)
2850 tty_gotoyx (1, xwidth
);
2851 tty_print_alt_char (ACS_TTEE
, FALSE
);
2852 tty_gotoyx (height
, xwidth
);
2853 tty_print_alt_char (ACS_BTEE
, FALSE
);
2854 tty_draw_vline (2, xwidth
, ACS_VLINE
, height
- 2);
2856 if (xwidth
< width2
- 1)
2858 tty_gotoyx (1, width1
+ xwidth
);
2859 tty_print_alt_char (ACS_TTEE
, FALSE
);
2860 tty_gotoyx (height
, width1
+ xwidth
);
2861 tty_print_alt_char (ACS_BTEE
, FALSE
);
2862 tty_draw_vline (2, width1
+ xwidth
, ACS_VLINE
, height
- 2);
2865 dview
->new_frame
= FALSE
;
2870 dview_status (dview
, dview
->ord
, width1
, 0);
2871 dview_display_file (dview
, dview
->ord
, 2, 1, height
- 2, width1
- 2);
2875 dview_status (dview
, dview
->ord
^ 1, width2
, width1
);
2876 dview_display_file (dview
, dview
->ord
^ 1, 2, width1
+ 1, height
- 2, width2
- 2);
2880 /* --------------------------------------------------------------------------------------------- */
2883 dview_edit (WDiff
* dview
, diff_place_t ord
)
2887 int linenum
, lineofs
;
2889 if (dview
->dsrc
== DATA_SRC_TMP
)
2891 error_dialog (_("Edit"), _("Edit is disabled"));
2895 h
= WIDGET (WIDGET (dview
)->owner
);
2896 h_modal
= widget_get_state (h
, WST_MODAL
);
2898 get_line_numbers (dview
->a
[ord
], dview
->skip_rows
, &linenum
, &lineofs
);
2900 /* disallow edit file in several editors */
2901 widget_set_state (h
, WST_MODAL
, TRUE
);
2904 vfs_path_t
*tmp_vpath
;
2906 tmp_vpath
= vfs_path_from_str (dview
->file
[ord
]);
2907 edit_file_at_line (tmp_vpath
, use_internal_edit
, linenum
);
2908 vfs_path_free (tmp_vpath
);
2911 widget_set_state (h
, WST_MODAL
, h_modal
);
2913 dview_update (dview
);
2916 /* --------------------------------------------------------------------------------------------- */
2919 dview_goto_cmd (WDiff
* dview
, diff_place_t ord
)
2921 static gboolean first_run
= TRUE
;
2924 static const char *title
[2] = {
2925 N_("Goto line (left)"),
2926 N_("Goto line (right)")
2934 input_dialog (_(title
[ord
]), _("Enter line:"), MC_HISTORY_YDIFF_GOTO_LINE
,
2935 first_run
? NULL
: INPUT_LAST_TEXT
, INPUT_COMPLETE_NONE
);
2938 const char *s
= input
;
2940 if (scan_deci (&s
, &newline
) == 0 && *s
== '\0')
2946 for (; i
< dview
->a
[ord
]->len
; i
++)
2950 p
= &g_array_index (dview
->a
[ord
], DIFFLN
, i
);
2951 if (p
->line
== newline
)
2955 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= (ssize_t
) i
;
2963 /* --------------------------------------------------------------------------------------------- */
2966 dview_labels (WDiff
* dview
)
2968 Widget
*d
= WIDGET (dview
);
2971 b
= find_buttonbar (DIALOG (d
->owner
));
2973 buttonbar_set_label (b
, 1, Q_ ("ButtonBar|Help"), diff_map
, d
);
2974 buttonbar_set_label (b
, 2, Q_ ("ButtonBar|Save"), diff_map
, d
);
2975 buttonbar_set_label (b
, 4, Q_ ("ButtonBar|Edit"), diff_map
, d
);
2976 buttonbar_set_label (b
, 5, Q_ ("ButtonBar|Merge"), diff_map
, d
);
2977 buttonbar_set_label (b
, 7, Q_ ("ButtonBar|Search"), diff_map
, d
);
2978 buttonbar_set_label (b
, 9, Q_ ("ButtonBar|Options"), diff_map
, d
);
2979 buttonbar_set_label (b
, 10, Q_ ("ButtonBar|Quit"), diff_map
, d
);
2982 /* --------------------------------------------------------------------------------------------- */
2985 dview_save (WDiff
* dview
)
2987 gboolean res
= TRUE
;
2989 if (dview
->merged
[DIFF_LEFT
])
2991 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
2992 dview
->merged
[DIFF_LEFT
] = !res
;
2994 if (dview
->merged
[DIFF_RIGHT
])
2996 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
2997 dview
->merged
[DIFF_RIGHT
] = !res
;
3002 /* --------------------------------------------------------------------------------------------- */
3005 dview_do_save (WDiff
* dview
)
3007 (void) dview_save (dview
);
3010 /* --------------------------------------------------------------------------------------------- */
3013 dview_save_options (WDiff
* dview
)
3015 mc_config_set_bool (mc_global
.main_config
, "DiffView", "show_symbols",
3016 dview
->display_symbols
!= 0);
3017 mc_config_set_bool (mc_global
.main_config
, "DiffView", "show_numbers",
3018 dview
->display_numbers
!= 0);
3019 mc_config_set_int (mc_global
.main_config
, "DiffView", "tab_size", dview
->tab_size
);
3021 mc_config_set_int (mc_global
.main_config
, "DiffView", "diff_quality", dview
->opt
.quality
);
3023 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_tws",
3024 dview
->opt
.strip_trailing_cr
);
3025 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_all_space",
3026 dview
->opt
.ignore_all_space
);
3027 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_space_change",
3028 dview
->opt
.ignore_space_change
);
3029 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_tab_expansion",
3030 dview
->opt
.ignore_tab_expansion
);
3031 mc_config_set_bool (mc_global
.main_config
, "DiffView", "diff_ignore_case",
3032 dview
->opt
.ignore_case
);
3035 /* --------------------------------------------------------------------------------------------- */
3038 dview_load_options (WDiff
* dview
)
3040 gboolean show_numbers
, show_symbols
;
3043 show_symbols
= mc_config_get_bool (mc_global
.main_config
, "DiffView", "show_symbols", FALSE
);
3045 dview
->display_symbols
= 1;
3046 show_numbers
= mc_config_get_bool (mc_global
.main_config
, "DiffView", "show_numbers", FALSE
);
3048 dview
->display_numbers
= calc_nwidth ((const GArray
* const *) dview
->a
);
3049 tab_size
= mc_config_get_int (mc_global
.main_config
, "DiffView", "tab_size", 8);
3050 if (tab_size
> 0 && tab_size
< 9)
3051 dview
->tab_size
= tab_size
;
3053 dview
->tab_size
= 8;
3055 dview
->opt
.quality
= mc_config_get_int (mc_global
.main_config
, "DiffView", "diff_quality", 0);
3057 dview
->opt
.strip_trailing_cr
=
3058 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_tws", FALSE
);
3059 dview
->opt
.ignore_all_space
=
3060 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_all_space", FALSE
);
3061 dview
->opt
.ignore_space_change
=
3062 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_space_change", FALSE
);
3063 dview
->opt
.ignore_tab_expansion
=
3064 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_tab_expansion", FALSE
);
3065 dview
->opt
.ignore_case
=
3066 mc_config_get_bool (mc_global
.main_config
, "DiffView", "diff_ignore_case", FALSE
);
3068 dview
->new_frame
= TRUE
;
3071 /* --------------------------------------------------------------------------------------------- */
3074 * Check if it's OK to close the diff viewer. If there are unsaved changes,
3078 dview_ok_to_exit (WDiff
* dview
)
3080 gboolean res
= TRUE
;
3083 if (!dview
->merged
[DIFF_LEFT
] && !dview
->merged
[DIFF_RIGHT
])
3086 act
= query_dialog (_("Quit"), !mc_global
.midnight_shutdown
?
3087 _("File(s) was modified. Save with exit?") :
3088 _("Midnight Commander is being shut down.\nSave modified file(s)?"),
3089 D_NORMAL
, 2, _("&Yes"), _("&No"));
3092 if (mc_global
.midnight_shutdown
|| (act
== -1))
3101 (void) dview_save (dview
);
3105 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~"))
3106 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_LEFT
], "~~~");
3107 if (mc_util_restore_from_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~"))
3108 res
= mc_util_unlink_backup_if_possible (dview
->file
[DIFF_RIGHT
], "~~~");
3117 /* --------------------------------------------------------------------------------------------- */
3120 dview_execute_cmd (WDiff
* dview
, long command
)
3122 cb_ret_t res
= MSG_HANDLED
;
3126 case CK_ShowSymbols
:
3127 dview
->display_symbols
^= 1;
3128 dview
->new_frame
= TRUE
;
3130 case CK_ShowNumbers
:
3131 dview
->display_numbers
^= calc_nwidth ((const GArray
* const *) dview
->a
);
3132 dview
->new_frame
= TRUE
;
3135 dview
->full
= !dview
->full
;
3136 dview
->new_frame
= TRUE
;
3142 dview
->new_frame
= TRUE
;
3148 dview_compute_split (dview
, 1);
3149 dview
->new_frame
= TRUE
;
3156 dview_compute_split (dview
, -1);
3157 dview
->new_frame
= TRUE
;
3161 dview
->tab_size
= 2;
3164 dview
->tab_size
= 3;
3167 dview
->tab_size
= 4;
3170 dview
->tab_size
= 8;
3179 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3180 find_next_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3183 dview
->skip_rows
= dview
->search
.last_accessed_num_line
=
3184 find_prev_hunk (dview
->a
[DIFF_LEFT
], dview
->skip_rows
);
3187 dview_goto_cmd (dview
, DIFF_RIGHT
);
3190 dview_edit (dview
, dview
->ord
);
3193 do_merge_hunk (dview
, FROM_LEFT_TO_RIGHT
);
3197 do_merge_hunk (dview
, FROM_RIGHT_TO_LEFT
);
3201 dview_edit (dview
, dview
->ord
^ 1);
3204 dview_search_cmd (dview
);
3206 case CK_SearchContinue
:
3207 dview_continue_search_cmd (dview
);
3210 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= 0;
3213 dview
->skip_rows
= dview
->search
.last_accessed_num_line
= dview
->a
[DIFF_LEFT
]->len
- 1;
3216 if (dview
->skip_rows
> 0)
3219 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3224 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3227 if (dview
->height
> 2)
3229 dview
->skip_rows
+= dview
->height
- 2;
3230 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3234 if (dview
->height
> 2)
3236 dview
->skip_rows
-= dview
->height
- 2;
3237 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3247 dview
->skip_cols
-= 8;
3250 dview
->skip_cols
+= 8;
3253 dview
->skip_cols
= 0;
3259 dview
->view_quit
= TRUE
;
3262 dview_do_save (dview
);
3265 dview_diff_options (dview
);
3268 case CK_SelectCodepage
:
3269 dview_select_encoding (dview
);
3273 /* don't close diffviewer due to SIGINT */
3276 res
= MSG_NOT_HANDLED
;
3281 /* --------------------------------------------------------------------------------------------- */
3284 dview_handle_key (WDiff
* dview
, int key
)
3289 key
= convert_from_input_c (key
);
3292 command
= keybind_lookup_keymap_command (diff_map
, key
);
3293 if ((command
!= CK_IgnoreKey
) && (dview_execute_cmd (dview
, command
) == MSG_HANDLED
))
3297 return MSG_NOT_HANDLED
;
3300 /* --------------------------------------------------------------------------------------------- */
3303 dview_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3305 WDiff
*dview
= (WDiff
*) w
;
3306 WDialog
*h
= DIALOG (w
->owner
);
3312 dview_labels (dview
);
3313 dview_load_options (dview
);
3314 dview_update (dview
);
3318 dview
->new_frame
= TRUE
;
3319 dview_update (dview
);
3323 i
= dview_handle_key (dview
, parm
);
3324 if (dview
->view_quit
)
3327 dview_update (dview
);
3331 i
= dview_execute_cmd (dview
, parm
);
3332 if (dview
->view_quit
)
3335 dview_update (dview
);
3339 dview_compute_areas (dview
);
3343 dview_save_options (dview
);
3348 return widget_default_callback (w
, sender
, msg
, parm
, data
);
3352 /* --------------------------------------------------------------------------------------------- */
3355 dview_mouse_callback (Widget
* w
, mouse_msg_t msg
, mouse_event_t
* event
)
3357 WDiff
*dview
= (WDiff
*) w
;
3363 case MSG_MOUSE_SCROLL_UP
:
3364 case MSG_MOUSE_SCROLL_DOWN
:
3365 if (msg
== MSG_MOUSE_SCROLL_UP
)
3366 dview
->skip_rows
-= 2;
3368 dview
->skip_rows
+= 2;
3370 dview
->search
.last_accessed_num_line
= dview
->skip_rows
;
3371 dview_update (dview
);
3379 /* --------------------------------------------------------------------------------------------- */
3382 dview_dialog_callback (Widget
* w
, Widget
* sender
, widget_msg_t msg
, int parm
, void *data
)
3385 WDialog
*h
= DIALOG (w
);
3390 /* Handle shortcuts. */
3392 /* Note: the buttonbar sends messages directly to the the WDiff, not to
3393 * here, which is why we can pass NULL in the following call. */
3394 return dview_execute_cmd (NULL
, parm
);
3397 dview
= (WDiff
*) find_widget_type (h
, dview_callback
);
3398 /* don't stop the dialog before final decision */
3399 widget_set_state (w
, WST_ACTIVE
, TRUE
);
3400 if (dview_ok_to_exit (dview
))
3405 return dlg_default_callback (w
, sender
, msg
, parm
, data
);
3409 /* --------------------------------------------------------------------------------------------- */
3412 dview_get_title (const WDialog
* h
, size_t len
)
3415 const char *modified
= " (*) ";
3416 const char *notmodified
= " ";
3420 dview
= (const WDiff
*) find_widget_type (h
, dview_callback
);
3421 len1
= (len
- str_term_width1 (_("Diff:")) - strlen (modified
) - 3) / 2;
3423 title
= g_string_sized_new (len
);
3424 g_string_append (title
, _("Diff:"));
3425 g_string_append (title
, dview
->merged
[DIFF_LEFT
] ? modified
: notmodified
);
3426 g_string_append (title
, str_term_trim (dview
->label
[DIFF_LEFT
], len1
));
3427 g_string_append (title
, " | ");
3428 g_string_append (title
, dview
->merged
[DIFF_RIGHT
] ? modified
: notmodified
);
3429 g_string_append (title
, str_term_trim (dview
->label
[DIFF_RIGHT
], len1
));
3431 return g_string_free (title
, FALSE
);
3434 /* --------------------------------------------------------------------------------------------- */
3437 diff_view (const char *file1
, const char *file2
, const char *label1
, const char *label2
)
3446 /* Create dialog and widgets, put them on the dialog */
3448 dlg_create (FALSE
, 0, 0, 1, 1, WPOS_FULLSCREEN
, FALSE
, NULL
, dview_dialog_callback
, NULL
,
3449 "[Diff Viewer]", NULL
);
3450 dw
= WIDGET (dview_dlg
);
3451 widget_want_tab (dw
, TRUE
);
3453 g
= GROUP (dview_dlg
);
3455 dview
= g_new0 (WDiff
, 1);
3457 widget_init (w
, dw
->y
, dw
->x
, dw
->lines
- 1, dw
->cols
, dview_callback
, dview_mouse_callback
);
3458 w
->options
|= WOP_SELECTABLE
;
3459 group_add_widget_autopos (g
, w
, WPOS_KEEP_ALL
, NULL
);
3461 w
= WIDGET (buttonbar_new (TRUE
));
3462 group_add_widget_autopos (g
, w
, w
->pos_flags
, NULL
);
3464 dview_dlg
->get_title
= dview_get_title
;
3466 error
= dview_init (dview
, "-a", file1
, file2
, label1
, label2
, DATA_SRC_MEM
); /* XXX binary diff? */
3469 dlg_run (dview_dlg
);
3471 if (error
!= 0 || widget_get_state (dw
, WST_CLOSED
))
3472 dlg_destroy (dview_dlg
);
3474 return error
== 0 ? 1 : 0;
3477 /*** public functions ****************************************************************************/
3478 /* --------------------------------------------------------------------------------------------- */
3480 #define GET_FILE_AND_STAMP(n) \
3484 real_file##n = file##n; \
3485 if (!vfs_file_is_local (file##n)) \
3487 real_file##n = mc_getlocalcopy (file##n); \
3488 if (real_file##n != NULL) \
3491 if (mc_stat (real_file##n, &st##n) != 0) \
3498 #define UNGET_FILE(n) \
3504 if (use_copy##n > 0) \
3507 mtime = st##n.st_mtime; \
3508 if (mc_stat (real_file##n, &st##n) == 0) \
3509 changed = (mtime != st##n.st_mtime); \
3511 mc_ungetlocalcopy (file##n, real_file##n, changed); \
3512 vfs_path_free (real_file##n); \
3518 dview_diff_cmd (const void *f0
, const void *f1
)
3521 vfs_path_t
*file0
= NULL
;
3522 vfs_path_t
*file1
= NULL
;
3523 gboolean is_dir0
= FALSE
;
3524 gboolean is_dir1
= FALSE
;
3526 switch (mc_global
.mc_run_mode
)
3530 /* run from panels */
3531 const WPanel
*panel0
= (const WPanel
*) f0
;
3532 const WPanel
*panel1
= (const WPanel
*) f1
;
3535 vfs_path_append_new (panel0
->cwd_vpath
, selection (panel0
)->fname
, (char *) NULL
);
3536 is_dir0
= S_ISDIR (selection (panel0
)->st
.st_mode
);
3539 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3540 path_trunc (selection (panel0
)->fname
, 30));
3545 vfs_path_append_new (panel1
->cwd_vpath
, selection (panel1
)->fname
, (char *) NULL
);
3546 is_dir1
= S_ISDIR (selection (panel1
)->st
.st_mode
);
3549 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"),
3550 path_trunc (selection (panel1
)->fname
, 30));
3556 case MC_RUN_DIFFVIEWER
:
3558 /* run from command line */
3559 const char *p0
= (const char *) f0
;
3560 const char *p1
= (const char *) f1
;
3563 file0
= vfs_path_from_str (p0
);
3564 if (mc_stat (file0
, &st
) == 0)
3566 is_dir0
= S_ISDIR (st
.st_mode
);
3569 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p0
, 30));
3575 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3576 path_trunc (p0
, 30), unix_error_string (errno
));
3580 file1
= vfs_path_from_str (p1
);
3581 if (mc_stat (file1
, &st
) == 0)
3583 is_dir1
= S_ISDIR (st
.st_mode
);
3586 message (D_ERROR
, MSG_ERROR
, _("\"%s\" is a directory"), path_trunc (p1
, 30));
3592 message (D_ERROR
, MSG_ERROR
, _("Cannot stat \"%s\"\n%s"),
3593 path_trunc (p1
, 30), unix_error_string (errno
));
3600 /* this should not happaned */
3601 message (D_ERROR
, MSG_ERROR
, _("Diff viewer: invalid mode"));
3608 if (file0
!= NULL
&& file1
!= NULL
)
3614 vfs_path_t
*real_file0
;
3615 vfs_path_t
*real_file1
;
3617 GET_FILE_AND_STAMP (0);
3618 GET_FILE_AND_STAMP (1);
3620 if (real_file0
!= NULL
&& real_file1
!= NULL
)
3621 rv
= diff_view (vfs_path_as_str (real_file0
), vfs_path_as_str (real_file1
),
3622 vfs_path_as_str (file0
), vfs_path_as_str (file1
));
3630 message (D_ERROR
, MSG_ERROR
, _("Two files are needed to compare"));
3633 vfs_path_free (file1
);
3634 vfs_path_free (file0
);
3639 #undef GET_FILE_AND_STAMP
3642 /* --------------------------------------------------------------------------------------------- */