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