Add defgroup; use defcustom for user vars.
[emacs.git] / lisp / ediff-ptch.el
blob309316d721e2a5a1943c137bb22936cd9b847015
1 ;;; ediff-ptch.el --- Ediff's patch support
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
7 ;; This file is part of GNU Emacs.
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to the
21 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 ;; Boston, MA 02111-1307, USA.
25 ;;; Code:
27 (require 'ediff-init)
29 (defvar ediff-last-dir-patch nil
30 "Last directory used by an Ediff command for file to patch.")
32 (defvar ediff-backup-extension
33 (if (memq system-type '(vax-vms axp-vms emx ms-dos windows-nt windows-95))
34 "_orig" ".orig")
35 "Backup extension used by the patch program.
36 See also `ediff-backup-specs'.")
38 (defvar ediff-backup-specs (format "-b %s" ediff-backup-extension)
39 "*Backup directives to pass to the patch program.
40 Ediff requires that the old version of the file \(before applying the patch\)
41 is saved in a file named `the-patch-file.extension'. Usually `extension' is
42 `.orig', but this can be changed by the user and may depend on the system.
43 Therefore, Ediff needs to know the backup extension used by the patch program.
45 Some versions of the patch program let you specify `-b backup-extension'.
46 Other versions only permit `-b', which assumes some canned extension
47 \(usually `.orig'\).
49 Note that both `ediff-backup-extension' and `ediff-backup-specs'
50 must be properly set. If your patch program takes the option `-b',
51 but not `-b extension', the variable `ediff-backup-extension' must
52 still be set so Ediff will know which extension to use.")
55 (defvar ediff-patch-default-directory nil
56 "*Default directory to look for patches.")
58 (defvar ediff-context-diff-label-regexp
59 (concat "\\(" ; context diff 2-liner
60 "^\\*\\*\\* \\([^ \t]+\\)[^*]+[\t ]*\n--- \\([^ \t]+\\)"
61 "\\|" ; GNU unified format diff 2-liner
62 "^--- \\([^ \t]+\\)[\t ]+.*\n\\+\\+\\+ \\([^ \t]+\\)"
63 "\\)")
64 "*Regexp matching filename 2-liners at the start of each context diff.")
66 (defvar ediff-patch-program "patch"
67 "*Name of the program that applies patches.
68 It is recommended to use GNU-compatible versions.")
69 (defvar ediff-patch-options "-f"
70 "*Options to pass to ediff-patch-program.
72 Note: the `-b' option should be specified in `ediff-backup-specs'.
74 It is recommended to pass the `-f' option to the patch program, so it won't ask
75 questions. However, some implementations don't accept this option, in which
76 case the default value for this variable should be changed.")
78 ;; The buffer of the patch file. Local to control buffer.
79 (ediff-defvar-local ediff-patchbufer nil "")
81 ;; The buffer where patch displays its diagnostics.
82 (ediff-defvar-local ediff-patch-diagnostics nil "")
84 ;; Map of patch buffer. Has the form:
85 ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
86 ;; where filenames are files to which patch would have applied the patch;
87 ;; marker1 delimits the beginning of the corresponding patch and marker2 does
88 ;; it for the end.
89 (ediff-defvar-local ediff-patch-map nil "")
91 ;; strip prefix from filename
92 ;; returns /dev/null, if can't strip prefix
93 (defsubst ediff-file-name-sans-prefix (filename prefix)
94 (save-match-data
95 (if (string-match (concat "^" prefix) filename)
96 (substring filename (match-end 0))
97 (concat "/null/" filename))))
101 ;; no longer used
102 ;; return the number of matches of regexp in buf starting from the beginning
103 (defun ediff-count-matches (regexp buf)
104 (ediff-eval-in-buffer buf
105 (let ((count 0) opoint)
106 (save-excursion
107 (goto-char (point-min))
108 (while (and (not (eobp))
109 (progn (setq opoint (point))
110 (re-search-forward regexp nil t)))
111 (if (= opoint (point))
112 (forward-char 1)
113 (setq count (1+ count)))))
114 count)))
116 ;; Scan BUF (which is supposed to contain a patch) and make a list of the form
117 ;; ((filename1 marker1 marker2) (filename2 marker1 marker2) ...)
118 ;; where filenames are files to which patch would have applied the patch;
119 ;; marker1 delimits the beginning of the corresponding patch and marker2 does
120 ;; it for the end. This list is then assigned to ediff-patch-map.
121 ;; Returns the number of elements in the list ediff-patch-map
122 (defun ediff-map-patch-buffer (buf)
123 (ediff-eval-in-buffer buf
124 (let ((count 0)
125 (mark1 (move-marker (make-marker) (point-min)))
126 (mark1-end (point-min))
127 (possible-file-names '("/dev/null" . "/dev/null"))
128 mark2-end mark2 filenames
129 beg1 beg2 end1 end2
130 patch-map opoint)
131 (save-excursion
132 (goto-char (point-min))
133 (setq opoint (point))
134 (while (and (not (eobp))
135 (re-search-forward ediff-context-diff-label-regexp nil t))
136 (if (= opoint (point))
137 (forward-char 1) ; ensure progress towards the end
138 (setq mark2 (move-marker (make-marker) (match-beginning 0))
139 mark2-end (match-end 0)
140 beg1 (or (match-beginning 2) (match-beginning 4))
141 end1 (or (match-end 2) (match-end 4))
142 beg2 (or (match-beginning 3) (match-beginning 5))
143 end2 (or (match-end 3) (match-end 5)))
144 ;; possible-file-names is holding the new file names until we
145 ;; insert the old file name in the patch map
146 ;; It is a pair (filename from 1st header line . fn from 2nd line)
147 (setq possible-file-names
148 (cons (if (and beg1 end1)
149 (buffer-substring beg1 end1)
150 "/dev/null")
151 (if (and beg2 end2)
152 (buffer-substring beg2 end2)
153 "/dev/null")))
154 ;; check for any `Index:' or `Prereq:' lines, but don't use them
155 (if (re-search-backward "^Index:" mark1-end 'noerror)
156 (move-marker mark2 (match-beginning 0)))
157 (if (re-search-backward "^Prereq:" mark1-end 'noerror)
158 (move-marker mark2 (match-beginning 0)))
160 (goto-char mark2-end)
162 (if filenames
163 (setq patch-map (cons (list filenames mark1 mark2) patch-map)))
164 (setq mark1 mark2
165 mark1-end mark2-end
166 filenames possible-file-names))
167 (setq opoint (point)
168 count (1+ count))))
169 (setq mark2 (point-max-marker)
170 patch-map (cons (list possible-file-names mark1 mark2) patch-map))
171 (setq ediff-patch-map (nreverse patch-map))
172 count)))
174 ;; Fix up the file names in the list using the argument FILENAME
175 ;; Algorithm: find the first file's directory and cut it out from each file
176 ;; name in the patch. Prepend the directory of FILENAME to each file in the
177 ;; patch. In addition, the first file in the patch is replaced by FILENAME.
178 ;; Each file is actually a file-pair of files found in the context diff header
179 ;; In the end, for each pair, we select the shortest existing file.
180 ;; Note: Ediff doesn't recognize multi-file patches that are separated
181 ;; with the `Index:' line. It treats them as a single-file patch.
183 ;; Executes inside the patch buffer
184 (defun ediff-fixup-patch-map (filename)
185 (setq filename (expand-file-name filename))
186 (let ((actual-dir (if (file-directory-p filename)
187 ;; directory part of filename
188 (file-name-as-directory filename)
189 (file-name-directory filename)))
190 ;; directory part of the first file in the patch
191 (base-dir1 (file-name-directory (car (car (car ediff-patch-map)))))
192 (base-dir2 (file-name-directory (cdr (car (car ediff-patch-map)))))
195 ;; chop off base-dirs
196 (mapcar (function (lambda (triple)
197 (or (string= (car (car triple)) "/dev/null")
198 (setcar (car triple)
199 (ediff-file-name-sans-prefix
200 (car (car triple)) base-dir1)))
201 (or (string= (cdr (car triple)) "/dev/null")
202 (setcdr (car triple)
203 (ediff-file-name-sans-prefix
204 (cdr (car triple)) base-dir2)))
206 ediff-patch-map)
208 ;; take the given file name into account
209 (or (file-directory-p filename)
210 (string= "/dev/null" filename)
211 (progn
212 (setcar (car ediff-patch-map)
213 (cons (file-name-nondirectory filename)
214 (file-name-nondirectory filename)))))
216 ;; prepend actual-dir
217 (mapcar (function (lambda (triple)
218 (if (and (string-match "^/null/" (car (car triple)))
219 (string-match "^/null/" (cdr (car triple))))
220 ;; couldn't strip base-dir1 and base-dir2
221 ;; hence, something wrong
222 (progn
223 (with-output-to-temp-buffer ediff-msg-buffer
224 (princ
225 (format "
226 The patch file contains a context diff for
231 However, Ediff cannot infer the name of the actual file
232 to be patched on your system. If you know the correct file name,
233 please enter it now.
235 If you don't know and still would like to apply patches to
236 other files, enter /dev/null
238 (substring (car (car triple)) 6)
239 (substring (cdr (car triple)) 6))))
240 (let ((directory t)
241 user-file)
242 (while directory
243 (setq user-file
244 (read-file-name
245 "Please enter file name: "
246 actual-dir actual-dir t))
247 (if (not (file-directory-p user-file))
248 (setq directory nil)
249 (setq directory t)
250 (beep)
251 (message "%s is a directory" user-file)
252 (sit-for 2)))
253 (setcar triple (cons user-file user-file))))
254 (setcar (car triple)
255 (expand-file-name
256 (concat actual-dir (car (car triple)))))
257 (setcdr (car triple)
258 (expand-file-name
259 (concat actual-dir (cdr (car triple))))))
261 ediff-patch-map)
262 ;; check for the shorter existing file in each pair and discard the other
263 ;; one
264 (mapcar (function (lambda (triple)
265 (let* ((file1 (car (car triple)))
266 (file2 (cdr (car triple)))
267 (f1-exists (file-exists-p file1))
268 (f2-exists (file-exists-p file2)))
269 (cond
270 ((and (< (length file2) (length file1))
271 f2-exists)
272 (setcar triple file2))
273 ((and (< (length file1) (length file2))
274 f1-exists)
275 (setcar triple file1))
276 ((and f1-exists f2-exists
277 (string= file1 file2))
278 (setcar triple file1))
279 ((and f1-exists f2-exists)
280 (with-output-to-temp-buffer ediff-msg-buffer
281 (princ (format "
282 Ediff has inferred that
285 are possible targets for applying the patch.
286 Both files seem to be plausible alternatives.
288 Please advice:
289 Type `y' to use %s as the target;
290 Type `n' to use %s as the target.
292 file1 file2 file2 file1)))
293 (setcar triple
294 (if (y-or-n-p (format "Use %s ? " file2))
295 file2 file1)))
296 (f2-exists (setcar triple file2))
297 (f1-exists (setcar triple file1))
299 (with-output-to-temp-buffer ediff-msg-buffer
300 (princ (format "
301 Ediff inferred that
304 are possible alternative targets for this patch.
306 However, these files do not exist.
308 Please enter an alternative patch target ...
310 file1 file2)))
311 (let ((directory t)
312 target)
313 (while directory
314 (setq target (read-file-name
315 "Please enter a patch target: "
316 actual-dir actual-dir t))
317 (if (not (file-directory-p target))
318 (setq directory nil)
319 (beep)
320 (message "%s is a directory" target)
321 (sit-for 2)))
322 (setcar triple target)))))))
323 ediff-patch-map)
326 (defun ediff-show-patch-diagnostics ()
327 (interactive)
328 (cond ((window-live-p ediff-window-A)
329 (set-window-buffer ediff-window-A ediff-patch-diagnostics))
330 ((window-live-p ediff-window-B)
331 (set-window-buffer ediff-window-B ediff-patch-diagnostics))
332 (t (display-buffer ediff-patch-diagnostics 'not-this-window))))
334 (defun ediff-get-patch-buffer ()
335 "Obtain patch buffer. If patch is already in a buffer---use it.
336 Else, read patch file into a new buffer."
337 (let ((dir (cond (ediff-patch-default-directory) ; try patch default dir
338 (ediff-use-last-dir ediff-last-dir-patch)
339 (t default-directory)))
340 patch-buf)
341 (if (y-or-n-p "Is the patch already in a buffer? ")
342 (setq patch-buf
343 (get-buffer
344 (read-buffer
345 "Which buffer contains the patch? "
346 (current-buffer) 'must-match)))
347 (setq patch-buf
348 (find-file-noselect
349 (read-file-name "Which file contains the patch? "
350 dir nil 'must-match))))
352 (ediff-eval-in-buffer patch-buf
353 (goto-char (point-min))
354 (or (ediff-get-visible-buffer-window patch-buf)
355 (progn
356 (pop-to-buffer patch-buf 'other-window)
357 (select-window (previous-window)))))
358 (ediff-map-patch-buffer patch-buf)
359 patch-buf))
361 ;; Dispatch the right patch file function: regular or meta-level,
362 ;; depending on how many patches are in the patch file.
363 ;; At present, there is no support for meta-level patches.
364 ;; Should return either the ctl buffer or the meta-buffer
365 (defun ediff-dispatch-file-patching-job (patch-buf filename
366 &optional startup-hooks)
367 (ediff-eval-in-buffer patch-buf
368 ;; relativize names in the patch with respect to source-file
369 (ediff-fixup-patch-map filename)
370 (if (< (length ediff-patch-map) 2)
371 (ediff-patch-file-internal
372 patch-buf
373 (if (and (not (string-match "^/dev/null" (car (car ediff-patch-map))))
374 (> (length (car (car ediff-patch-map))) 1))
375 (car (car ediff-patch-map))
376 filename)
377 startup-hooks)
378 (ediff-multi-patch-internal patch-buf startup-hooks))
382 (defun ediff-patch-buffer-internal (patch-buf buf-to-patch-name
383 &optional startup-hooks)
384 (let* ((buf-to-patch (get-buffer buf-to-patch-name))
385 (file-name-ok (if buf-to-patch (buffer-file-name buf-to-patch)))
386 (buf-mod-status (buffer-modified-p buf-to-patch))
387 (multifile-patch-p (> (length (ediff-eval-in-buffer patch-buf
388 ediff-patch-map)) 1))
389 default-dir file-name ctl-buf)
390 (if file-name-ok
391 (setq file-name file-name-ok)
392 (if multifile-patch-p
393 (error
394 "Can't apply multi-file patches to buffers that visit no files"))
395 (ediff-eval-in-buffer buf-to-patch
396 (setq default-dir default-directory)
397 (setq file-name (ediff-make-temp-file buf-to-patch))
398 (set-visited-file-name file-name)
399 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
400 ;;don't confuse the user with a new bufname
401 (rename-buffer buf-to-patch-name)
402 (set-buffer-modified-p nil)
403 (set-visited-file-modtime) ; sync buffer and temp file
404 (setq default-directory default-dir)
407 ;; dispatch a patch function
408 (setq ctl-buf (ediff-dispatch-file-patching-job
409 patch-buf file-name startup-hooks))
411 (if file-name-ok
413 ;; buffer wasn't visiting any file,
414 ;; so we will not run meta-level ediff here
415 (ediff-eval-in-buffer ctl-buf
416 (delete-file (buffer-file-name ediff-buffer-A))
417 (delete-file (buffer-file-name ediff-buffer-B))
418 (ediff-eval-in-buffer ediff-buffer-A
419 (if default-dir (setq default-directory default-dir))
420 (set-visited-file-name nil)
421 (rename-buffer buf-to-patch-name)
422 (set-buffer-modified-p buf-mod-status))
423 (ediff-eval-in-buffer ediff-buffer-B
424 (setq buffer-auto-save-file-name nil) ; don't create auto-save file
425 (if default-dir (setq default-directory default-dir))
426 (set-visited-file-name nil)
427 (rename-buffer (ediff-unique-buffer-name
428 (concat buf-to-patch-name "_patched") ""))
429 (set-buffer-modified-p t))))
432 (defun ediff-patch-file-internal (patch-buf source-filename
433 &optional startup-hooks)
434 (setq source-filename (expand-file-name source-filename))
436 (let* ((shell-file-name ediff-shell)
437 (patch-diagnostics (get-buffer-create "*ediff patch diagnostics*"))
438 ;; ediff-find-file may use a temp file to do the patch
439 ;; so, we save source-filename and true-source-filename as a var
440 ;; that initially is source-filename but may be changed to a temp
441 ;; file for the purpose of patching.
442 (true-source-filename source-filename)
443 (target-filename source-filename)
444 target-buf buf-to-patch file-name-magic-p
445 patch-return-code ctl-buf backup-style aux-wind)
447 (if (string-match "-V" ediff-patch-options)
448 (error
449 "Ediff doesn't take the -V option in `ediff-patch-options'--sorry"))
451 ;; Make a temp file, if source-filename has a magic file handler (or if
452 ;; it is handled via auto-mode-alist and similar magic).
453 ;; Check if there is a buffer visiting source-filename and if they are in
454 ;; sync; arrange for the deletion of temp file.
455 (ediff-find-file 'true-source-filename 'buf-to-patch
456 'ediff-last-dir-patch 'startup-hooks)
458 ;; Check if source file name has triggered black magic, such as file name
459 ;; handlers or auto mode alist, and make a note of it.
460 ;; true-source-filename should be either the original name or a
461 ;; temporary file where we put the after-product of the file handler.
462 (setq file-name-magic-p (not (equal (file-truename true-source-filename)
463 (file-truename source-filename))))
465 ;; Checkout orig file, if necessary, so that the patched file
466 ;; could be checked back in.
467 (ediff-maybe-checkout buf-to-patch)
469 (ediff-eval-in-buffer patch-diagnostics
470 (insert-buffer patch-buf)
471 (message "Applying patch ... ")
472 ;; fix environment for gnu patch, so it won't make numbered extensions
473 (setq backup-style (getenv "VERSION_CONTROL"))
474 (setenv "VERSION_CONTROL" nil)
475 (setq patch-return-code
476 (call-process-region
477 (point-min) (point-max)
478 shell-file-name
479 t ; delete region (which contains the patch
480 t ; insert output (patch diagnostics) in current buffer
481 nil ; don't redisplay
482 shell-command-switch ; usually -c
483 (format "%s %s %s %s"
484 ediff-patch-program
485 ediff-patch-options
486 ediff-backup-specs
487 (expand-file-name true-source-filename))
490 ;; restore environment for gnu patch
491 (setenv "VERSION_CONTROL" backup-style))
493 (message "Applying patch ... done")
494 (message "")
496 (switch-to-buffer patch-diagnostics)
497 (sit-for 0) ; synchronize - let the user see diagnostics
499 (or (and (eq patch-return-code 0) ; patch reported success
500 (file-exists-p
501 (concat true-source-filename ediff-backup-extension)))
502 (progn
503 (with-output-to-temp-buffer ediff-msg-buffer
504 (princ (format "
505 Patch has failed OR the backup version of the patched file was not created by
506 the patch program.
508 One reason may be that the values of the variables
510 ediff-patch-options = %S
511 ediff-backup-extension = %S
512 ediff-backup-specs = %S
514 are not appropriate for the program specified in the variable
516 ediff-patch-program = %S
518 Another reason could be that the %S program doesn't understand
519 the format of the patch file you used.
521 See Ediff on-line manual for more details on these variables.
522 \(Or use a GNU-compatible patch program and stay out of trouble.\)
524 Type any key to continue...
526 ediff-patch-options
527 ediff-backup-extension
528 ediff-backup-specs
529 ediff-patch-program
530 ediff-patch-program)))
531 (beep 1)
532 (if (setq aux-wind (get-buffer-window ediff-msg-buffer))
533 (progn
534 (select-window aux-wind)
535 (goto-char (point-max))))
536 (read-char-exclusive)
537 (if aux-wind (bury-buffer)) ; ediff-msg-buffer
538 (if (setq aux-wind (get-buffer-window patch-diagnostics))
539 (progn
540 (select-window aux-wind)
541 (bury-buffer)))
542 (error "Patch appears to have failed")))
544 ;; If black magic is involved, apply patch to a temp copy of the
545 ;; file. Otherwise, apply patch to the orig copy. If patch is applied
546 ;; to temp copy, we name the result old-name_patched for local files
547 ;; and temp-copy_patched for remote files. The orig file name isn't
548 ;; changed, and the temp copy of the original is later deleted.
549 ;; Without magic, the original file is renamed (usually into
550 ;; old-name_orig) and the result of patching will have the same name as
551 ;; the original.
552 (if (not file-name-magic-p)
553 (ediff-eval-in-buffer buf-to-patch
554 (set-visited-file-name
555 (concat source-filename ediff-backup-extension))
556 (set-buffer-modified-p nil))
558 ;; Black magic in effect.
559 ;; If orig file was remote, put the patched file in the temp directory.
560 ;; If orig file is local, put the patched file in the directory of
561 ;; the orig file.
562 (setq target-filename
563 (concat
564 (if (ediff-file-remote-p (file-truename source-filename))
565 true-source-filename
566 source-filename)
567 "_patched"))
569 (rename-file true-source-filename target-filename t)
571 ;; arrange that the temp copy of orig will be deleted
572 (rename-file (concat true-source-filename ediff-backup-extension)
573 true-source-filename t))
575 ;; make orig buffer read-only
576 (setq startup-hooks
577 (cons 'ediff-set-read-only-in-buf-A startup-hooks))
579 ;; set up a buf for the patched file
580 (setq target-buf (find-file-noselect target-filename))
582 (setq ctl-buf
583 (ediff-buffers-internal
584 buf-to-patch target-buf nil
585 startup-hooks 'epatch))
586 (ediff-eval-in-buffer ctl-buf
587 (setq ediff-patchbufer patch-buf
588 ediff-patch-diagnostics patch-diagnostics))
590 (bury-buffer patch-diagnostics)
591 (message "Type `P', if you need to see patch diagnostics")
592 ctl-buf))
594 (defun ediff-multi-patch-internal (patch-buf &optional startup-hooks)
595 (let (meta-buf)
596 (setq startup-hooks
597 ;; this sets various vars in the meta buffer inside
598 ;; ediff-prepare-meta-buffer
599 (cons (` (lambda ()
600 ;; tell what to do if the user clicks on a session record
601 (setq ediff-session-action-function
602 'ediff-patch-file-form-meta
603 ediff-meta-patchbufer patch-buf)
605 startup-hooks))
606 (setq meta-buf (ediff-prepare-meta-buffer
607 'ediff-filegroup-action
608 (ediff-eval-in-buffer patch-buf
609 ;; nil replaces a regular expression
610 (cons (list nil (format "%S" patch-buf))
611 ediff-patch-map))
612 "*Ediff Session Group Panel"
613 'ediff-redraw-directory-group-buffer
614 'ediff-multifile-patch
615 startup-hooks))
616 (ediff-show-meta-buffer meta-buf)
622 ;;; Local Variables:
623 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
624 ;;; eval: (put 'ediff-eval-in-buffer 'lisp-indent-hook 1)
625 ;;; eval: (put 'ediff-eval-in-buffer 'edebug-form-spec '(form body))
626 ;;; End:
628 (provide 'ediff-ptch)
630 ;;; ediff-ptch.el ends here