Merged from emacs@sv.gnu.org.
[bpt/emacs.git] / lisp / ediff-ptch.el
CommitLineData
fce30d79
MK
1;;; ediff-ptch.el --- Ediff's patch support
2
0d30b337 3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002,
aaef169d 4;; 2003, 2004, 2005, 2006 Free Software Foundation, Inc.
fce30d79 5
50a07e18 6;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
fce30d79
MK
7
8;; This file is part of GNU Emacs.
9
10;; GNU Emacs is free software; you can redistribute it and/or modify
11;; it under the terms of the GNU General Public License as published by
12;; the Free Software Foundation; either version 2, or (at your option)
13;; any later version.
14
15;; GNU Emacs is distributed in the hope that it will be useful,
16;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18;; GNU General Public License for more details.
19
20;; You should have received a copy of the GNU General Public License
21;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
22;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23;; Boston, MA 02110-1301, USA.
fce30d79 24
3afbc435 25;;; Commentary:
fce30d79
MK
26
27;;; Code:
71296446 28
ddc90f39
MK
29
30(defgroup ediff-ptch nil
143b42a6 31 "Ediff patch support."
ddc90f39
MK
32 :tag "Patch"
33 :prefix "ediff-"
34 :group 'ediff)
35
36;; compiler pacifier
37(defvar ediff-window-A)
38(defvar ediff-window-B)
39(defvar ediff-window-C)
40(defvar ediff-use-last-dir)
41(defvar ediff-shell)
42
43(eval-when-compile
44 (let ((load-path (cons (expand-file-name ".") load-path)))
45 (or (featurep 'ediff-init)
46 (load "ediff-init.el" nil nil 'nosuffix))
743a79af
MK
47 (or (featurep 'ediff-mult)
48 (load "ediff-mult.el" nil nil 'nosuffix))
ddc90f39
MK
49 (or (featurep 'ediff)
50 (load "ediff.el" nil nil 'nosuffix))
51 ))
52;; end pacifier
fce30d79 53
4b45b44f
RS
54(require 'ediff-init)
55
1e70790f
MK
56(defcustom ediff-patch-program "patch"
57 "*Name of the program that applies patches.
58It is recommended to use GNU-compatible versions."
59 :type 'string
60 :group 'ediff-ptch)
61(defcustom ediff-patch-options "-f"
62 "*Options to pass to ediff-patch-program.
63
64Note: the `-b' option should be specified in `ediff-backup-specs'.
65
66It is recommended to pass the `-f' option to the patch program, so it won't ask
3af0304a 67questions. However, some implementations don't accept this option, in which
1e70790f
MK
68case the default value for this variable should be changed."
69 :type 'string
70 :group 'ediff-ptch)
71
fce30d79
MK
72(defvar ediff-last-dir-patch nil
73 "Last directory used by an Ediff command for file to patch.")
74
1e70790f
MK
75;; the default backup extension
76(defconst ediff-default-backup-extension
77 (if (memq system-type '(vax-vms axp-vms emx ms-dos))
78 "_orig" ".orig"))
71296446 79
1e70790f
MK
80
81(defcustom ediff-backup-extension ediff-default-backup-extension
92c51e07 82 "Backup extension used by the patch program.
1e70790f
MK
83See also `ediff-backup-specs'."
84 :type 'string
85 :group 'ediff-ptch)
92c51e07 86
bd698e98 87(defun ediff-test-patch-utility ()
d29a70fe 88 (condition-case nil
15502042 89 (cond ((eq 0 (call-process ediff-patch-program nil nil nil "-z." "-b"))
d29a70fe
RS
90 ;; GNU `patch' v. >= 2.2
91 'gnu)
15502042 92 ((eq 0 (call-process ediff-patch-program nil nil nil "-b"))
d29a70fe
RS
93 'posix)
94 (t 'traditional))
95 (file-error nil)))
bd698e98 96
71296446 97(defcustom ediff-backup-specs
bd698e98
MK
98 (let ((type (ediff-test-patch-utility)))
99 (cond ((eq type 'gnu)
100 ;; GNU `patch' v. >= 2.2
101 (format "-z%s -b" ediff-backup-extension))
102 ((eq type 'posix)
103 ;; POSIX `patch' -- ediff-backup-extension must be ".orig"
104 (setq ediff-backup-extension ediff-default-backup-extension)
105 "-b")
106 (t
107 ;; traditional `patch'
108 (format "-b %s" ediff-backup-extension))))
92c51e07
MK
109 "*Backup directives to pass to the patch program.
110Ediff requires that the old version of the file \(before applying the patch\)
3af0304a 111be saved in a file named `the-patch-file.extension'. Usually `extension' is
92c51e07
MK
112`.orig', but this can be changed by the user and may depend on the system.
113Therefore, Ediff needs to know the backup extension used by the patch program.
114
115Some versions of the patch program let you specify `-b backup-extension'.
1e70790f 116Other versions only permit `-b', which assumes the extension `.orig'
3af0304a 117\(in which case ediff-backup-extension MUST be also `.orig'\). The latest
1e70790f 118versions of GNU patch require `-b -z backup-extension'.
92c51e07
MK
119
120Note that both `ediff-backup-extension' and `ediff-backup-specs'
3af0304a 121must be set properly. If your patch program takes the option `-b',
92c51e07 122but not `-b extension', the variable `ediff-backup-extension' must
bd698e98
MK
123still be set so Ediff will know which extension to use.
124
3af0304a 125Ediff tries to guess the appropriate value for this variables. It is believed
bd698e98 126to be working for `traditional' patch, all versions of GNU patch, and for POSIX
3af0304a 127patch. So, don't change these variables, unless the default doesn't work."
ddc90f39
MK
128 :type 'string
129 :group 'ediff-ptch)
92c51e07 130
fce30d79 131
ddc90f39
MK
132(defcustom ediff-patch-default-directory nil
133 "*Default directory to look for patches."
134 :type '(choice (const nil) string)
135 :group 'ediff-ptch)
fce30d79 136
ddc90f39 137(defcustom ediff-context-diff-label-regexp
fce30d79
MK
138 (concat "\\(" ; context diff 2-liner
139 "^\\*\\*\\* \\([^ \t]+\\)[^*]+[\t ]*\n--- \\([^ \t]+\\)"
140 "\\|" ; GNU unified format diff 2-liner
a8b7f4b9 141 "^--- \\([^ \t]+\\)[\t ]+.*\n\\+\\+\\+ \\([^ \t]+\\)"
fce30d79 142 "\\)")
1e70790f
MK
143 "*Regexp matching filename 2-liners at the start of each context diff.
144You probably don't want to change that, unless you are using an obscure patch
145program."
ddc90f39
MK
146 :type 'regexp
147 :group 'ediff-ptch)
fce30d79 148
3af0304a 149;; The buffer of the patch file. Local to control buffer.
fce30d79
MK
150(ediff-defvar-local ediff-patchbufer nil "")
151
152;; The buffer where patch displays its diagnostics.
153(ediff-defvar-local ediff-patch-diagnostics nil "")
154
3af0304a 155;; Map of patch buffer. Has the form:
fce30d79
MK
156;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
157;; where filenames are files to which patch would have applied the patch;
158;; marker1 delimits the beginning of the corresponding patch and marker2 does
159;; it for the end.
160(ediff-defvar-local ediff-patch-map nil "")
161
162;; strip prefix from filename
163;; returns /dev/null, if can't strip prefix
164(defsubst ediff-file-name-sans-prefix (filename prefix)
15c77b9e
MK
165 (if prefix
166 (save-match-data
167 (if (string-match (concat "^" (if (stringp prefix)
168 (regexp-quote prefix)
169 ""))
170 filename)
171 (substring filename (match-end 0))
172 (concat "/null/" filename)))
173 filename)
174 )
fce30d79
MK
175
176
177
178;; no longer used
179;; return the number of matches of regexp in buf starting from the beginning
180(defun ediff-count-matches (regexp buf)
e756eb9f 181 (ediff-with-current-buffer buf
fce30d79
MK
182 (let ((count 0) opoint)
183 (save-excursion
184 (goto-char (point-min))
185 (while (and (not (eobp))
186 (progn (setq opoint (point))
187 (re-search-forward regexp nil t)))
188 (if (= opoint (point))
189 (forward-char 1)
190 (setq count (1+ count)))))
191 count)))
192
71296446 193;; Scan BUF (which is supposed to contain a patch) and make a list of the form
743a79af
MK
194;; ((nil nil filename-spec1 marker1 marker2)
195;; (nil nil filename-spec2 marker1 marker2) ...)
71296446 196;; where filename-spec[12] are files to which the `patch' program would
743a79af
MK
197;; have applied the patch.
198;; nin, nil are placeholders. See ediff-make-new-meta-list-element in
199;; ediff-meta.el for the explanations.
200;; In the beginning we don't know exactly which files need to be patched.
201;; We usually come up with two candidates and ediff-file-name-sans-prefix
202;; resolves this later.
203;;
204;; The marker `marker1' delimits the beginning of the corresponding patch and
205;; `marker2' does it for the end.
206;; The result of ediff-map-patch-buffer is a list, which is then assigned
207;; to ediff-patch-map.
208;; The function returns the number of elements in the list ediff-patch-map
fce30d79 209(defun ediff-map-patch-buffer (buf)
e756eb9f 210 (ediff-with-current-buffer buf
fce30d79
MK
211 (let ((count 0)
212 (mark1 (move-marker (make-marker) (point-min)))
213 (mark1-end (point-min))
214 (possible-file-names '("/dev/null" . "/dev/null"))
215 mark2-end mark2 filenames
216 beg1 beg2 end1 end2
217 patch-map opoint)
218 (save-excursion
219 (goto-char (point-min))
220 (setq opoint (point))
221 (while (and (not (eobp))
222 (re-search-forward ediff-context-diff-label-regexp nil t))
223 (if (= opoint (point))
224 (forward-char 1) ; ensure progress towards the end
225 (setq mark2 (move-marker (make-marker) (match-beginning 0))
226 mark2-end (match-end 0)
a8b7f4b9 227 beg1 (or (match-beginning 2) (match-beginning 4))
92c51e07
MK
228 end1 (or (match-end 2) (match-end 4))
229 beg2 (or (match-beginning 3) (match-beginning 5))
230 end2 (or (match-end 3) (match-end 5)))
fce30d79
MK
231 ;; possible-file-names is holding the new file names until we
232 ;; insert the old file name in the patch map
743a79af
MK
233 ;; It is a pair
234 ;; (filename-from-1st-header-line . fn from 2nd line)
fce30d79
MK
235 (setq possible-file-names
236 (cons (if (and beg1 end1)
237 (buffer-substring beg1 end1)
238 "/dev/null")
239 (if (and beg2 end2)
240 (buffer-substring beg2 end2)
241 "/dev/null")))
242 ;; check for any `Index:' or `Prereq:' lines, but don't use them
243 (if (re-search-backward "^Index:" mark1-end 'noerror)
244 (move-marker mark2 (match-beginning 0)))
245 (if (re-search-backward "^Prereq:" mark1-end 'noerror)
246 (move-marker mark2 (match-beginning 0)))
247
248 (goto-char mark2-end)
71296446 249
fce30d79 250 (if filenames
743a79af
MK
251 (setq patch-map
252 (cons (ediff-make-new-meta-list-element
253 filenames mark1 mark2)
254 patch-map)))
fce30d79
MK
255 (setq mark1 mark2
256 mark1-end mark2-end
257 filenames possible-file-names))
258 (setq opoint (point)
259 count (1+ count))))
260 (setq mark2 (point-max-marker)
743a79af
MK
261 patch-map (cons (ediff-make-new-meta-list-element
262 possible-file-names mark1 mark2)
263 patch-map))
fce30d79
MK
264 (setq ediff-patch-map (nreverse patch-map))
265 count)))
266
267;; Fix up the file names in the list using the argument FILENAME
15c77b9e
MK
268;; Algorithm: find the files' directories in the patch and, if a directory is
269;; absolute, cut it out from the corresponding file name in the patch.
270;; Relative directories are not cut out.
271;; Prepend the directory of FILENAME to each resulting file (which came
272;; originally from the patch).
273;; In addition, the first file in the patch document is replaced by FILENAME.
274;; Each file is actually a pair of files found in the context diff header
275;; In the end, for each pair, we ask the user which file to patch.
fce30d79 276;; Note: Ediff doesn't recognize multi-file patches that are separated
3af0304a 277;; with the `Index:' line. It treats them as a single-file patch.
fce30d79
MK
278;;
279;; Executes inside the patch buffer
280(defun ediff-fixup-patch-map (filename)
281 (setq filename (expand-file-name filename))
282 (let ((actual-dir (if (file-directory-p filename)
283 ;; directory part of filename
284 (file-name-as-directory filename)
285 (file-name-directory filename)))
15c77b9e
MK
286 ;; In case 2 files are possible patch targets, the user will be offered
287 ;; to choose file1 or file2. In a multifile patch, if the user chooses
288 ;; 1 or 2, this choice is preserved to decide future alternatives.
289 chosen-alternative
fce30d79
MK
290 )
291
292 ;; chop off base-dirs
743a79af 293 (mapcar (lambda (session-info)
15c77b9e
MK
294 (let* ((proposed-file-names
295 ;; Filename-spec is objA; it is represented as
296 ;; (file1 . file2). Get it using ediff-get-session-objA.
297 (ediff-get-session-objA-name session-info))
298 ;; base-dir1 is the dir part of the 1st file in the patch
e2de3a29
MK
299 (base-dir1
300 (or (file-name-directory (car proposed-file-names))
301 ""))
15c77b9e 302 ;; directory part of the 2nd file in the patch
e2de3a29
MK
303 (base-dir2
304 (or (file-name-directory (cdr proposed-file-names))
305 ""))
15c77b9e 306 )
e2de3a29
MK
307 ;; If both base-dir1 and base-dir2 are relative and exist,
308 ;; assume that
15c77b9e
MK
309 ;; these dirs lead to the actual files starting at the present
310 ;; directory. So, we don't strip these relative dirs from the
311 ;; file names. This is a heuristic intended to improve guessing
e2de3a29
MK
312 (unless (or (file-name-absolute-p base-dir1)
313 (file-name-absolute-p base-dir2)
314 (not (file-exists-p base-dir1))
315 (not (file-exists-p base-dir2)))
15c77b9e
MK
316 (setq base-dir1 ""
317 base-dir2 ""))
743a79af
MK
318 (or (string= (car proposed-file-names) "/dev/null")
319 (setcar proposed-file-names
320 (ediff-file-name-sans-prefix
321 (car proposed-file-names) base-dir1)))
15c77b9e
MK
322 (or (string=
323 (cdr proposed-file-names) "/dev/null")
324 (setcdr proposed-file-names
325 (ediff-file-name-sans-prefix
326 (cdr proposed-file-names) base-dir2)))
327 ))
fce30d79
MK
328 ediff-patch-map)
329
330 ;; take the given file name into account
331 (or (file-directory-p filename)
332 (string= "/dev/null" filename)
743a79af
MK
333 (setcar (ediff-get-session-objA (car ediff-patch-map))
334 (cons (file-name-nondirectory filename)
335 (file-name-nondirectory filename))))
fce30d79
MK
336
337 ;; prepend actual-dir
743a79af
MK
338 (mapcar (lambda (session-info)
339 (let ((proposed-file-names
340 (ediff-get-session-objA-name session-info)))
341 (if (and (string-match "^/null/" (car proposed-file-names))
342 (string-match "^/null/" (cdr proposed-file-names)))
15c77b9e
MK
343 ;; couldn't intuit the file name to patch, so
344 ;; something is amiss
743a79af
MK
345 (progn
346 (with-output-to-temp-buffer ediff-msg-buffer
347 (ediff-with-current-buffer standard-output
348 (fundamental-mode))
349 (princ
350 (format "
fce30d79
MK
351The patch file contains a context diff for
352 %s
353 %s
fce30d79 354However, Ediff cannot infer the name of the actual file
3af0304a 355to be patched on your system. If you know the correct file name,
fce30d79
MK
356please enter it now.
357
358If you don't know and still would like to apply patches to
359other files, enter /dev/null
360"
743a79af
MK
361 (substring (car proposed-file-names) 6)
362 (substring (cdr proposed-file-names) 6))))
363 (let ((directory t)
364 user-file)
365 (while directory
366 (setq user-file
367 (read-file-name
368 "Please enter file name: "
369 actual-dir actual-dir t))
370 (if (not (file-directory-p user-file))
371 (setq directory nil)
372 (setq directory t)
373 (beep)
374 (message "%s is a directory" user-file)
375 (sit-for 2)))
376 (setcar (ediff-get-session-objA session-info)
377 (cons user-file user-file))))
378 (setcar proposed-file-names
71296446 379 (expand-file-name
743a79af
MK
380 (concat actual-dir (car proposed-file-names))))
381 (setcdr proposed-file-names
71296446 382 (expand-file-name
743a79af
MK
383 (concat actual-dir (cdr proposed-file-names)))))
384 ))
fce30d79 385 ediff-patch-map)
e2de3a29
MK
386 ;; Check for the existing files in each pair and discard the nonexisting
387 ;; ones. If both exist, ask the user.
743a79af
MK
388 (mapcar (lambda (session-info)
389 (let* ((file1 (car (ediff-get-session-objA-name session-info)))
390 (file2 (cdr (ediff-get-session-objA-name session-info)))
391 (session-file-object
392 (ediff-get-session-objA session-info))
3af0304a
MK
393 (f1-exists (file-exists-p file1))
394 (f2-exists (file-exists-p file2)))
395 (cond
15c77b9e
MK
396 ((and
397 ;; The patch program prefers the shortest file as the patch
398 ;; target. However, this is a questionable heuristic. In an
399 ;; interactive program, like ediff, we can offer the user a
400 ;; choice.
401 ;; (< (length file2) (length file1))
402 (not f1-exists)
403 f2-exists)
743a79af
MK
404 ;; replace file-pair with the winning file2
405 (setcar session-file-object file2))
15c77b9e
MK
406 ((and
407 ;; (< (length file1) (length file2))
408 (not f2-exists)
409 f1-exists)
743a79af
MK
410 ;; replace file-pair with the winning file1
411 (setcar session-file-object file1))
3af0304a
MK
412 ((and f1-exists f2-exists
413 (string= file1 file2))
743a79af 414 (setcar session-file-object file1))
15c77b9e
MK
415 ((and f1-exists f2-exists (eq chosen-alternative 1))
416 (setcar session-file-object file1))
417 ((and f1-exists f2-exists (eq chosen-alternative 2))
418 (setcar session-file-object file2))
3af0304a
MK
419 ((and f1-exists f2-exists)
420 (with-output-to-temp-buffer ediff-msg-buffer
2acc9e43
DL
421 (ediff-with-current-buffer standard-output
422 (fundamental-mode))
3af0304a 423 (princ (format "
fce30d79
MK
424Ediff has inferred that
425 %s
426 %s
bd698e98 427are two possible targets for applying the patch.
fce30d79
MK
428Both files seem to be plausible alternatives.
429
430Please advice:
431 Type `y' to use %s as the target;
432 Type `n' to use %s as the target.
433"
15c77b9e 434 file1 file2 file1 file2)))
743a79af 435 (setcar session-file-object
15c77b9e
MK
436 (if (y-or-n-p (format "Use %s ? " file1))
437 (progn
438 (setq chosen-alternative 1)
439 file1)
440 (setq chosen-alternative 2)
441 file2))
442 )
743a79af
MK
443 (f2-exists (setcar session-file-object file2))
444 (f1-exists (setcar session-file-object file1))
3af0304a
MK
445 (t
446 (with-output-to-temp-buffer ediff-msg-buffer
2acc9e43
DL
447 (ediff-with-current-buffer standard-output
448 (fundamental-mode))
3af0304a
MK
449 (princ "\nEdiff has inferred that")
450 (if (string= file1 file2)
451 (princ (format "
fce30d79 452 %s
15c77b9e 453is assumed to be the target for this patch. However, this file does not exist."
3af0304a
MK
454 file1))
455 (princ (format "
fce30d79 456 %s
bd698e98 457 %s
3af0304a
MK
458are two possible targets for this patch. However, these files do not exist."
459 file1 file2)))
460 (princ "
bd698e98 461\nPlease enter an alternative patch target ...\n"))
3af0304a
MK
462 (let ((directory t)
463 target)
464 (while directory
71296446 465 (setq target (read-file-name
3af0304a
MK
466 "Please enter a patch target: "
467 actual-dir actual-dir t))
468 (if (not (file-directory-p target))
469 (setq directory nil)
470 (beep)
471 (message "%s is a directory" target)
472 (sit-for 2)))
743a79af 473 (setcar session-file-object target))))))
fce30d79
MK
474 ediff-patch-map)
475 ))
476
477(defun ediff-show-patch-diagnostics ()
478 (interactive)
479 (cond ((window-live-p ediff-window-A)
480 (set-window-buffer ediff-window-A ediff-patch-diagnostics))
481 ((window-live-p ediff-window-B)
482 (set-window-buffer ediff-window-B ediff-patch-diagnostics))
483 (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
484
3af0304a
MK
485;; prompt for file, get the buffer
486(defun ediff-prompt-for-patch-file ()
15c77b9e
MK
487 (let ((dir (cond (ediff-use-last-dir ediff-last-dir-patch)
488 (ediff-patch-default-directory) ; try patch default dir
4960e757 489 (t default-directory)))
15c77b9e
MK
490 (coding-system-for-read ediff-coding-system-for-read)
491 patch-file-name)
492 (setq patch-file-name
493 (read-file-name
5b76833f 494 (format "Patch is in file%s: "
15c77b9e
MK
495 (cond ((and buffer-file-name
496 (equal (expand-file-name dir)
497 (file-name-directory buffer-file-name)))
498 (concat
499 " (default "
500 (file-name-nondirectory buffer-file-name)
501 ")"))
502 (t "")))
503 dir buffer-file-name 'must-match))
504 (if (file-directory-p patch-file-name)
505 (error "Patch file cannot be a directory: %s" patch-file-name)
506 (find-file-noselect patch-file-name))
3af0304a
MK
507 ))
508
509
510;; Try current buffer, then the other window's buffer. Else, give up.
511(defun ediff-prompt-for-patch-buffer ()
512 (get-buffer
513 (read-buffer
7261ece3 514 "Buffer that holds the patch: "
3af0304a
MK
515 (cond ((save-excursion
516 (goto-char (point-min))
517 (re-search-forward ediff-context-diff-label-regexp nil t))
518 (current-buffer))
519 ((save-window-excursion
520 (other-window 1)
521 (save-excursion
522 (goto-char (point-min))
523 (and (re-search-forward ediff-context-diff-label-regexp nil t)
524 (current-buffer)))))
525 ((save-window-excursion
526 (other-window -1)
527 (save-excursion
528 (goto-char (point-min))
529 (and (re-search-forward ediff-context-diff-label-regexp nil t)
530 (current-buffer)))))
4960e757 531 (t (ediff-other-buffer (current-buffer))))
3af0304a
MK
532 'must-match)))
533
534
535(defun ediff-get-patch-buffer (&optional arg patch-buf)
536 "Obtain patch buffer. If patch is already in a buffer---use it.
537Else, read patch file into a new buffer. If patch buffer is passed as an
538optional argument, then use it."
539 (let ((last-nonmenu-event t) ; Emacs: don't use dialog box
540 last-command-event) ; XEmacs: don't use dialog box
541
542 (cond ((ediff-buffer-live-p patch-buf))
543 ;; even prefix arg: patch in buffer
544 ((and (integerp arg) (eq 0 (mod arg 2)))
545 (setq patch-buf (ediff-prompt-for-patch-buffer)))
546 ;; odd prefix arg: get patch from a file
547 ((and (integerp arg) (eq 1 (mod arg 2)))
548 (setq patch-buf (ediff-prompt-for-patch-file)))
549 (t (setq patch-buf
550 (if (y-or-n-p "Is the patch already in a buffer? ")
551 (ediff-prompt-for-patch-buffer)
552 (ediff-prompt-for-patch-file)))))
71296446 553
e756eb9f 554 (ediff-with-current-buffer patch-buf
fce30d79
MK
555 (goto-char (point-min))
556 (or (ediff-get-visible-buffer-window patch-buf)
557 (progn
558 (pop-to-buffer patch-buf 'other-window)
559 (select-window (previous-window)))))
560 (ediff-map-patch-buffer patch-buf)
561 patch-buf))
562
563;; Dispatch the right patch file function: regular or meta-level,
564;; depending on how many patches are in the patch file.
565;; At present, there is no support for meta-level patches.
566;; Should return either the ctl buffer or the meta-buffer
567(defun ediff-dispatch-file-patching-job (patch-buf filename
568 &optional startup-hooks)
e756eb9f 569 (ediff-with-current-buffer patch-buf
fce30d79
MK
570 ;; relativize names in the patch with respect to source-file
571 (ediff-fixup-patch-map filename)
572 (if (< (length ediff-patch-map) 2)
573 (ediff-patch-file-internal
574 patch-buf
2acc9e43 575 (if (and ediff-patch-map
743a79af
MK
576 (not (string-match
577 "^/dev/null"
578 ;; this is the file to patch
579 (ediff-get-session-objA-name (car ediff-patch-map))))
71296446 580 (> (length
743a79af
MK
581 (ediff-get-session-objA-name (car ediff-patch-map)))
582 1))
583 (ediff-get-session-objA-name (car ediff-patch-map))
fce30d79
MK
584 filename)
585 startup-hooks)
586 (ediff-multi-patch-internal patch-buf startup-hooks))
587 ))
588
589
3af0304a 590;; When patching a buffer, never change the orig file. Instead, create a new
bd698e98
MK
591;; buffer, ***_patched, even if the buff visits a file.
592;; Users who want to actually patch the buffer should use
593;; ediff-patch-file, not ediff-patch-buffer.
594(defun ediff-patch-buffer-internal (patch-buf
595 buf-to-patch-name
596 &optional startup-hooks)
fce30d79 597 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
bd698e98 598 (visited-file (if buf-to-patch (buffer-file-name buf-to-patch)))
fce30d79 599 (buf-mod-status (buffer-modified-p buf-to-patch))
e756eb9f 600 (multifile-patch-p (> (length (ediff-with-current-buffer patch-buf
fce30d79
MK
601 ediff-patch-map)) 1))
602 default-dir file-name ctl-buf)
bd698e98
MK
603 (if multifile-patch-p
604 (error
3af0304a 605 "To apply multi-file patches, please use `ediff-patch-file'"))
bd698e98
MK
606
607 ;; create a temp file to patch
608 (ediff-with-current-buffer buf-to-patch
609 (setq default-dir default-directory)
610 (setq file-name (ediff-make-temp-file buf-to-patch))
611 ;; temporarily switch visited file name, if any
612 (set-visited-file-name file-name)
613 ;; don't create auto-save file, if buff was visiting a file
614 (or visited-file
615 (setq buffer-auto-save-file-name nil))
616 ;; don't confuse the user with a new bufname
617 (rename-buffer buf-to-patch-name)
618 (set-buffer-modified-p nil)
619 (set-visited-file-modtime) ; sync buffer and temp file
620 (setq default-directory default-dir)
621 )
71296446 622
fce30d79
MK
623 ;; dispatch a patch function
624 (setq ctl-buf (ediff-dispatch-file-patching-job
625 patch-buf file-name startup-hooks))
71296446 626
bd698e98
MK
627 (ediff-with-current-buffer ctl-buf
628 (delete-file (buffer-file-name ediff-buffer-A))
629 (delete-file (buffer-file-name ediff-buffer-B))
630 (ediff-with-current-buffer ediff-buffer-A
631 (if default-dir (setq default-directory default-dir))
632 (set-visited-file-name visited-file) ; visited-file might be nil
633 (rename-buffer buf-to-patch-name)
634 (set-buffer-modified-p buf-mod-status))
635 (ediff-with-current-buffer ediff-buffer-B
636 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
637 (if default-dir (setq default-directory default-dir))
638 (set-visited-file-name nil)
71296446 639 (rename-buffer (ediff-unique-buffer-name
bd698e98
MK
640 (concat buf-to-patch-name "_patched") ""))
641 (set-buffer-modified-p t)))
fce30d79
MK
642 ))
643
bd698e98
MK
644
645;; Traditional patch has weird return codes.
646;; GNU and Posix return 1 if some hanks failed and 2 in case of trouble.
647;; 0 is a good code in all cases.
648;; We'll do the concervative thing.
649(defun ediff-patch-return-code-ok (code)
650 (eq code 0))
651;;; (if (eq (ediff-test-patch-utility) 'traditional)
652;;; (eq code 0)
653;;; (not (eq code 2))))
654
fce30d79
MK
655(defun ediff-patch-file-internal (patch-buf source-filename
656 &optional startup-hooks)
657 (setq source-filename (expand-file-name source-filename))
71296446 658
92c51e07 659 (let* ((shell-file-name ediff-shell)
fce30d79
MK
660 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
661 ;; ediff-find-file may use a temp file to do the patch
662 ;; so, we save source-filename and true-source-filename as a var
663 ;; that initially is source-filename but may be changed to a temp
664 ;; file for the purpose of patching.
665 (true-source-filename source-filename)
666 (target-filename source-filename)
4960e757
MK
667 ;; this ensures that the patch process gets patch buffer in the
668 ;; encoding that Emacs thinks is right for that type of text
71296446 669 (coding-system-for-write
4960e757 670 (if (boundp 'buffer-file-coding-system) buffer-file-coding-system))
71296446 671 target-buf buf-to-patch file-name-magic-p
e756eb9f 672 patch-return-code ctl-buf backup-style aux-wind)
71296446 673
bd698e98 674 (if (string-match "V" ediff-patch-options)
fce30d79
MK
675 (error
676 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
71296446 677
fce30d79
MK
678 ;; Make a temp file, if source-filename has a magic file handler (or if
679 ;; it is handled via auto-mode-alist and similar magic).
680 ;; Check if there is a buffer visiting source-filename and if they are in
681 ;; sync; arrange for the deletion of temp file.
682 (ediff-find-file 'true-source-filename 'buf-to-patch
683 'ediff-last-dir-patch 'startup-hooks)
684
685 ;; Check if source file name has triggered black magic, such as file name
686 ;; handlers or auto mode alist, and make a note of it.
687 ;; true-source-filename should be either the original name or a
688 ;; temporary file where we put the after-product of the file handler.
689 (setq file-name-magic-p (not (equal (file-truename true-source-filename)
690 (file-truename source-filename))))
71296446
JB
691
692 ;; Checkout orig file, if necessary, so that the patched file
bf5d92c5
MK
693 ;; could be checked back in.
694 (ediff-maybe-checkout buf-to-patch)
fce30d79 695
e756eb9f 696 (ediff-with-current-buffer patch-diagnostics
15c77b9e 697 (insert-buffer-substring patch-buf)
fce30d79
MK
698 (message "Applying patch ... ")
699 ;; fix environment for gnu patch, so it won't make numbered extensions
700 (setq backup-style (getenv "VERSION_CONTROL"))
701 (setenv "VERSION_CONTROL" nil)
92c51e07
MK
702 (setq patch-return-code
703 (call-process-region
704 (point-min) (point-max)
705 shell-file-name
706 t ; delete region (which contains the patch
707 t ; insert output (patch diagnostics) in current buffer
708 nil ; don't redisplay
709 shell-command-switch ; usually -c
710 (format "%s %s %s %s"
711 ediff-patch-program
712 ediff-patch-options
713 ediff-backup-specs
714 (expand-file-name true-source-filename))
715 ))
716
fce30d79
MK
717 ;; restore environment for gnu patch
718 (setenv "VERSION_CONTROL" backup-style))
719
720 (message "Applying patch ... done")
721 (message "")
722
723 (switch-to-buffer patch-diagnostics)
724 (sit-for 0) ; synchronize - let the user see diagnostics
71296446 725
bd698e98 726 (or (and (ediff-patch-return-code-ok patch-return-code)
92c51e07
MK
727 (file-exists-p
728 (concat true-source-filename ediff-backup-extension)))
729 (progn
730 (with-output-to-temp-buffer ediff-msg-buffer
2acc9e43
DL
731 (ediff-with-current-buffer standard-output
732 (fundamental-mode))
71296446 733 (princ (format
bd698e98
MK
734 "Patch program has failed due to a bad patch file,
735it couldn't apply all hunks, OR
736it couldn't create the backup for the file being patched.
92c51e07 737
1e70790f
MK
738The former could be caused by a corrupt patch file or because the %S
739program doesn't understand the format of the patch file in use.
92c51e07 740
1e70790f 741The second problem might be due to an incompatibility among these settings:
bd698e98
MK
742 ediff-patch-program = %S ediff-patch-options = %S
743 ediff-backup-extension = %S ediff-backup-specs = %S
92c51e07 744
92c51e07 745See Ediff on-line manual for more details on these variables.
71296446 746In particular, check the documentation for `ediff-backup-specs'.
bd698e98
MK
747
748In any of the above cases, Ediff doesn't compare files automatically.
749However, if the patch was applied partially and the backup file was created,
750you can still examine the changes via M-x ediff-files"
2acc9e43
DL
751 ediff-patch-program
752 ediff-patch-program
753 ediff-patch-options
754 ediff-backup-extension
755 ediff-backup-specs
756 )))
92c51e07
MK
757 (beep 1)
758 (if (setq aux-wind (get-buffer-window ediff-msg-buffer))
759 (progn
760 (select-window aux-wind)
761 (goto-char (point-max))))
1e70790f 762 (switch-to-buffer-other-window patch-diagnostics)
92c51e07 763 (error "Patch appears to have failed")))
71296446 764
fce30d79 765 ;; If black magic is involved, apply patch to a temp copy of the
3af0304a 766 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
fce30d79 767 ;; to temp copy, we name the result old-name_patched for local files
3af0304a 768 ;; and temp-copy_patched for remote files. The orig file name isn't
fce30d79
MK
769 ;; changed, and the temp copy of the original is later deleted.
770 ;; Without magic, the original file is renamed (usually into
771 ;; old-name_orig) and the result of patching will have the same name as
772 ;; the original.
773 (if (not file-name-magic-p)
e756eb9f 774 (ediff-with-current-buffer buf-to-patch
92c51e07
MK
775 (set-visited-file-name
776 (concat source-filename ediff-backup-extension))
fce30d79 777 (set-buffer-modified-p nil))
71296446 778
fce30d79
MK
779 ;; Black magic in effect.
780 ;; If orig file was remote, put the patched file in the temp directory.
781 ;; If orig file is local, put the patched file in the directory of
782 ;; the orig file.
783 (setq target-filename
784 (concat
785 (if (ediff-file-remote-p (file-truename source-filename))
786 true-source-filename
787 source-filename)
788 "_patched"))
71296446 789
fce30d79 790 (rename-file true-source-filename target-filename t)
71296446 791
fce30d79 792 ;; arrange that the temp copy of orig will be deleted
92c51e07 793 (rename-file (concat true-source-filename ediff-backup-extension)
fce30d79 794 true-source-filename t))
71296446 795
fce30d79
MK
796 ;; make orig buffer read-only
797 (setq startup-hooks
798 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
71296446 799
fce30d79
MK
800 ;; set up a buf for the patched file
801 (setq target-buf (find-file-noselect target-filename))
71296446 802
fce30d79
MK
803 (setq ctl-buf
804 (ediff-buffers-internal
805 buf-to-patch target-buf nil
806 startup-hooks 'epatch))
e756eb9f 807 (ediff-with-current-buffer ctl-buf
fce30d79
MK
808 (setq ediff-patchbufer patch-buf
809 ediff-patch-diagnostics patch-diagnostics))
71296446 810
fce30d79
MK
811 (bury-buffer patch-diagnostics)
812 (message "Type `P', if you need to see patch diagnostics")
813 ctl-buf))
814
815(defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
816 (let (meta-buf)
817 (setq startup-hooks
818 ;; this sets various vars in the meta buffer inside
819 ;; ediff-prepare-meta-buffer
086171bf
MK
820 (cons `(lambda ()
821 ;; tell what to do if the user clicks on a session record
822 (setq ediff-session-action-function
823 'ediff-patch-file-form-meta
824 ediff-meta-patchbufer patch-buf) )
fce30d79 825 startup-hooks))
71296446 826 (setq meta-buf (ediff-prepare-meta-buffer
fce30d79 827 'ediff-filegroup-action
e756eb9f 828 (ediff-with-current-buffer patch-buf
743a79af
MK
829 (cons (ediff-make-new-meta-list-header
830 nil ; regexp
831 (format "%S" patch-buf) ; obj A
832 nil nil ; objects B,C
833 nil ; merge-auto-store-dir
834 nil ; comparison-func
835 )
fce30d79
MK
836 ediff-patch-map))
837 "*Ediff Session Group Panel"
838 'ediff-redraw-directory-group-buffer
839 'ediff-multifile-patch
840 startup-hooks))
841 (ediff-show-meta-buffer meta-buf)
842 ))
843
71296446
JB
844
845
b6178721
MK
846(provide 'ediff-ptch)
847
fce30d79
MK
848
849;;; Local Variables:
850;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
e756eb9f
MK
851;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
852;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
fce30d79
MK
853;;; End:
854
ab5796a9 855;;; arch-tag: 2fe2161e-e116-469b-90fa-5cbb44c1bd1b
fce30d79 856;;; ediff-ptch.el ends here