(ad-make-advised-definition): Fix arg-order.
[emacs.git] / lisp / ediff-merg.el
blobb08238467df45d8c787570fa8164d7948fde8fcf
1 ;;; ediff-merg.el --- merging utilities
3 ;; Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002,
4 ;; 2003, 2004, 2005 Free Software Foundation, Inc.
6 ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
8 ;; This file is part of GNU Emacs.
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.
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.
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
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;;; Code:
29 (provide 'ediff-merg)
31 ;; compiler pacifier
32 (defvar ediff-window-A)
33 (defvar ediff-window-B)
34 (defvar ediff-window-C)
35 (defvar ediff-merge-window-share)
36 (defvar ediff-window-config-saved)
38 (eval-when-compile
39 (let ((load-path (cons (expand-file-name ".") load-path)))
40 (or (featurep 'ediff-init)
41 (load "ediff-init.el" nil nil 'nosuffix))
42 (or (featurep 'ediff-util)
43 (load "ediff-util.el" nil nil 'nosuffix))
45 ;; end pacifier
47 (require 'ediff-init)
49 (defcustom ediff-quit-merge-hook 'ediff-maybe-save-and-delete-merge
50 "*Hooks to run before quitting a merge job.
51 The most common use is to save and delete the merge buffer."
52 :type 'hook
53 :group 'ediff-merge)
56 (defcustom ediff-default-variant 'combined
57 "*The variant to be used as a default for buffer C in merging.
58 Valid values are the symbols `default-A', `default-B', and `combined'."
59 :type '(radio (const default-A) (const default-B) (const combined))
60 :group 'ediff-merge)
62 (defcustom ediff-combination-pattern
63 '("<<<<<<< variant A" A ">>>>>>> variant B" B "####### Ancestor" Ancestor "======= end")
64 "*Pattern to be used for combining difference regions in buffers A and B.
65 The value must be a list of the form
66 (STRING1 bufspec1 STRING2 bufspec2 STRING3 bufspec3 STRING4)
67 where bufspec is the symbol A, B, or Ancestor. For instance, if the value is
68 '(STRING1 A STRING2 Ancestor STRING3 B STRING4) then the
69 combined text will look like this:
71 STRING1
72 diff region from variant A
73 STRING2
74 diff region from the ancestor
75 STRING3
76 diff region from variant B
77 STRING4
79 :type '(choice (list string symbol string symbol string)
80 (list string symbol string symbol string symbol string))
81 :group 'ediff-merge)
83 (defcustom ediff-show-clashes-only nil
84 "*If t, show only those diff regions where both buffers disagree with the ancestor.
85 This means that regions that have status prefer-A or prefer-B will be
86 skipped over. nil means show all regions."
87 :type 'boolean
88 :group 'ediff-merge
90 (make-variable-buffer-local 'ediff-show-clashes-only)
92 (defcustom ediff-skip-merge-regions-that-differ-from-default nil
93 "*If t, show only the regions that have not been changed by the user.
94 A region is considered to have been changed if it is different from the current
95 default (`default-A', `default-B', `combined') and it hasn't been marked as
96 `prefer-A' or `prefer-B'.
97 A region is considered to have been changed also when it is marked as
98 as `prefer-A', but is different from the corresponding difference region in
99 Buffer A or if it is marked as `prefer-B' and is different from the region in
100 Buffer B."
101 :type 'boolean
102 :group 'ediff-merge
104 (make-variable-buffer-local 'ediff-skip-merge-regions-that-differ-from-default)
106 ;; check if there is no clash between the ancestor and one of the variants.
107 (defsubst ediff-merge-region-is-non-clash (n)
108 (string-match "prefer" (or (ediff-get-state-of-merge n) "")))
110 ;; If ediff-show-clashes-only, check if there is no clash between the ancestor
111 ;; and one of the variants.
112 (defsubst ediff-merge-region-is-non-clash-to-skip (n)
113 (and ediff-show-clashes-only
114 (ediff-merge-region-is-non-clash n)))
116 ;; If ediff-skip-changed-regions, check if the merge region differs from
117 ;; the current default. If a region is different from the default, it means
118 ;; that the user has made determination as to how to merge for this particular
119 ;; region.
120 (defsubst ediff-skip-merge-region-if-changed-from-default-p (n)
121 (and ediff-skip-merge-regions-that-differ-from-default
122 (ediff-merge-changed-from-default-p n 'prefers-too)))
125 (defun ediff-get-combined-region (n)
126 (let ((pattern-list ediff-combination-pattern)
127 (combo-region "")
128 (err-msg
129 "ediff-combination-pattern: Invalid format. Please consult the documentation")
130 region-delim region-spec)
132 (if (< (length pattern-list) 5)
133 (error err-msg))
135 (while (> (length pattern-list) 2)
136 (setq region-delim (nth 0 pattern-list)
137 region-spec (nth 1 pattern-list))
138 (or (and (stringp region-delim) (memq region-spec '(A B Ancestor)))
139 (error err-msg))
141 (condition-case nil
142 (setq combo-region
143 (concat combo-region
144 region-delim "\n"
145 (ediff-get-region-contents
146 n region-spec ediff-control-buffer)))
147 (error ""))
148 (setq pattern-list (cdr (cdr pattern-list)))
151 (setq region-delim (nth 0 pattern-list))
152 (or (stringp region-delim)
153 (error err-msg))
154 (setq combo-region (concat combo-region region-delim "\n"))
157 ;;(defsubst ediff-make-combined-diff (regA regB)
158 ;; (concat (nth 0 ediff-combination-pattern) "\n"
159 ;; regA
160 ;; (nth 1 ediff-combination-pattern) "\n"
161 ;; regB
162 ;; (nth 2 ediff-combination-pattern) "\n"))
164 (defsubst ediff-set-state-of-all-diffs-in-all-buffers (ctl-buf)
165 (let ((n 0))
166 (while (< n ediff-number-of-differences)
167 (ediff-set-state-of-diff-in-all-buffers n ctl-buf)
168 (setq n (1+ n)))))
170 (defun ediff-set-state-of-diff-in-all-buffers (n ctl-buf)
171 (let ((regA (ediff-get-region-contents n 'A ctl-buf))
172 (regB (ediff-get-region-contents n 'B ctl-buf))
173 (regC (ediff-get-region-contents n 'C ctl-buf)))
174 (cond ((and (string= regA regB) (string= regA regC))
175 (ediff-set-state-of-diff n 'A "=diff(B)")
176 (ediff-set-state-of-diff n 'B "=diff(C)")
177 (ediff-set-state-of-diff n 'C "=diff(A)"))
178 ((string= regA regB)
179 (ediff-set-state-of-diff n 'A "=diff(B)")
180 (ediff-set-state-of-diff n 'B "=diff(A)")
181 (ediff-set-state-of-diff n 'C nil))
182 ((string= regA regC)
183 (ediff-set-state-of-diff n 'A "=diff(C)")
184 (ediff-set-state-of-diff n 'C "=diff(A)")
185 (ediff-set-state-of-diff n 'B nil))
186 ((string= regB regC)
187 (ediff-set-state-of-diff n 'C "=diff(B)")
188 (ediff-set-state-of-diff n 'B "=diff(C)")
189 (ediff-set-state-of-diff n 'A nil))
190 ((string= regC (ediff-get-combined-region n))
191 (ediff-set-state-of-diff n 'A nil)
192 (ediff-set-state-of-diff n 'B nil)
193 (ediff-set-state-of-diff n 'C "=diff(A+B)"))
194 (t (ediff-set-state-of-diff n 'A nil)
195 (ediff-set-state-of-diff n 'B nil)
196 (ediff-set-state-of-diff n 'C nil)))
199 (defun ediff-set-merge-mode ()
200 (normal-mode t)
201 (remove-hook 'local-write-file-hooks 'ediff-set-merge-mode))
204 ;; Go over all diffs starting with DIFF-NUM and copy regions into buffer C
205 ;; according to the state of the difference.
206 ;; Since ediff-copy-diff refuses to copy identical diff regions, there is
207 ;; no need to optimize ediff-do-merge any further.
209 ;; If re-merging, change state of merge in all diffs starting with
210 ;; DIFF-NUM, except those where the state is prefer-* or where it is
211 ;; `default-*' or `combined' but the buf C region appears to be modified
212 ;; since last set by default.
213 (defun ediff-do-merge (diff-num &optional remerging)
214 (if (< diff-num 0) (setq diff-num 0))
215 (let ((n diff-num)
216 ;;(default-state-of-merge (format "%S" ediff-default-variant))
217 do-not-copy state-of-merge)
218 (while (< n ediff-number-of-differences)
219 (setq do-not-copy nil) ; reset after each cycle
220 (if (= (mod n 10) 0)
221 (message "%s buffers A & B into C ... region %d of %d"
222 (if remerging "Re-merging" "Merging")
224 ediff-number-of-differences))
226 (setq state-of-merge (ediff-get-state-of-merge n))
228 (if remerging
229 ;;(let ((reg-A (ediff-get-region-contents n 'A ediff-control-buffer))
230 ;; (reg-B (ediff-get-region-contents n 'B ediff-control-buffer))
231 ;; (reg-C (ediff-get-region-contents n 'C ediff-control-buffer)))
232 (let ()
234 ;; if region was edited since it was first set by default
235 (if (or (ediff-merge-changed-from-default-p n)
236 ;; was preferred
237 (string-match "prefer" state-of-merge))
238 ;; then ignore
239 (setq do-not-copy t))
241 ;; change state of merge for this diff, if necessary
242 (if (and (string-match "\\(default\\|combined\\)" state-of-merge)
243 (not do-not-copy))
244 (ediff-set-state-of-merge
245 n (format "%S" ediff-default-variant)))
248 ;; state-of-merge may have changed via ediff-set-state-of-merge, so
249 ;; check it once again
250 (setq state-of-merge (ediff-get-state-of-merge n))
252 (or do-not-copy
253 (if (string= state-of-merge "combined")
254 ;; use n+1 because ediff-combine-diffs works via user numbering
255 ;; of diffs, which is 1+ to what ediff uses internally
256 (ediff-combine-diffs (1+ n) 'batch)
257 (ediff-copy-diff
258 n (if (string-match "-A" state-of-merge) 'A 'B) 'C 'batch)))
259 (setq n (1+ n)))
260 (message "Merging buffers A & B into C ... Done")
264 (defun ediff-re-merge ()
265 "Remerge unmodified diff regions using a new default. Start with the current region."
266 (interactive)
267 (let* ((default-variant-alist
268 (list '("default-A") '("default-B") '("combined")))
269 (actual-alist
270 (delete (list (symbol-name ediff-default-variant))
271 default-variant-alist)))
272 (setq ediff-default-variant
273 (intern
274 (completing-read
275 (format "Current merge default is `%S'. New default: "
276 ediff-default-variant)
277 actual-alist nil 'must-match)))
278 (ediff-do-merge ediff-current-difference 'remerge)
279 (ediff-recenter)
282 (defun ediff-shrink-window-C (arg)
283 "Shrink window C to just one line.
284 With a prefix argument, returns window C to its normal size.
285 Used only for merging jobs."
286 (interactive "P")
287 (if (not ediff-merge-job)
288 (error "ediff-shrink-window-C can be used only for merging jobs"))
289 (cond ((eq arg '-) (setq arg -1))
290 ((not (numberp arg)) (setq arg nil)))
291 (cond ((null arg)
292 (let ((ediff-merge-window-share
293 (if (< (window-height ediff-window-C) 3)
294 ediff-merge-window-share 0)))
295 (setq ediff-window-config-saved "") ; force redisplay
296 (ediff-recenter 'no-rehighlight)))
297 ((and (< arg 0) (> (window-height ediff-window-C) 2))
298 (setq ediff-merge-window-share (* ediff-merge-window-share 0.9))
299 (setq ediff-window-config-saved "") ; force redisplay
300 (ediff-recenter 'no-rehighlight))
301 ((and (> arg 0) (> (window-height ediff-window-A) 2))
302 (setq ediff-merge-window-share (* ediff-merge-window-share 1.1))
303 (setq ediff-window-config-saved "") ; force redisplay
304 (ediff-recenter 'no-rehighlight))))
307 ;; N here is the user's region number. It is 1+ what Ediff uses internally.
308 (defun ediff-combine-diffs (n &optional batch-invocation)
309 "Combine Nth diff regions of buffers A and B and place the combination in C.
310 N is a prefix argument. If nil, combine the current difference regions.
311 Combining is done according to the specifications in variable
312 `ediff-combination-pattern'."
313 (interactive "P")
314 (setq n (if (numberp n) (1- n) ediff-current-difference))
316 (let (reg-combined)
317 ;;(setq regA (ediff-get-region-contents n 'A ediff-control-buffer)
318 ;; regB (ediff-get-region-contents n 'B ediff-control-buffer))
319 ;;(setq reg-combined (ediff-make-combined-diff regA regB))
320 (setq reg-combined (ediff-get-combined-region n))
322 (ediff-copy-diff n nil 'C batch-invocation reg-combined))
323 (or batch-invocation (ediff-jump-to-difference (1+ n))))
326 ;; Checks if the region in buff C looks like a combination of the regions
327 ;; in buffers A and B. Return a list (reg-a-beg reg-a-end reg-b-beg reg-b-end)
328 ;; These refer to where the delimiters for region A, B, Ancestor start and end
329 ;; in buffer C
330 (defun ediff-looks-like-combined-merge (region-num)
331 (if ediff-merge-job
332 (let ((combined (string-match (regexp-quote "(A+B)")
333 (or (ediff-get-state-of-diff region-num 'C)
334 "")))
335 (mrgreg-beg (ediff-get-diff-posn 'C 'beg region-num))
336 (mrgreg-end (ediff-get-diff-posn 'C 'end region-num))
337 (pattern-list ediff-combination-pattern)
338 delim reg-beg reg-end delim-regs-list)
340 (if combined
341 (ediff-with-current-buffer ediff-buffer-C
342 (while pattern-list
343 (goto-char mrgreg-beg)
344 (setq delim (nth 0 pattern-list))
345 (search-forward delim mrgreg-end 'noerror)
346 (setq reg-beg (match-beginning 0))
347 (setq reg-end (match-end 0))
348 (if (and reg-beg reg-end)
349 (setq delim-regs-list
350 ;; in reverse
351 (cons reg-end (cons reg-beg delim-regs-list))))
352 (if (> (length pattern-list) 1)
353 (setq pattern-list (cdr (cdr pattern-list)))
354 (setq pattern-list nil))
357 (reverse delim-regs-list)
360 (eval-when-compile (defvar state-of-merge)) ; dynamic var
362 ;; Check if the non-preferred merge has been modified since originally set.
363 ;; This affects only the regions that are marked as default-A/B or combined.
364 ;; If PREFERS-TOO is non-nil, then look at the regions marked as prefers-A/B as
365 ;; well.
366 (defun ediff-merge-changed-from-default-p (diff-num &optional prefers-too)
367 (let ((reg-A (ediff-get-region-contents diff-num 'A ediff-control-buffer))
368 (reg-B (ediff-get-region-contents diff-num 'B ediff-control-buffer))
369 (reg-C (ediff-get-region-contents diff-num 'C ediff-control-buffer)))
371 (setq state-of-merge (ediff-get-state-of-merge diff-num))
373 ;; if region was edited since it was first set by default
374 (or (and (string= state-of-merge "default-A")
375 (not (string= reg-A reg-C)))
376 (and (string= state-of-merge "default-B")
377 (not (string= reg-B reg-C)))
378 (and (string= state-of-merge "combined")
379 ;;(not (string= (ediff-make-combined-diff reg-A reg-B) reg-C)))
380 (not (string= (ediff-get-combined-region diff-num) reg-C)))
381 (and prefers-too
382 (string= state-of-merge "prefer-A")
383 (not (string= reg-A reg-C)))
384 (and prefers-too
385 (string= state-of-merge "prefer-B")
386 (not (string= reg-B reg-C)))
390 ;;; Local Variables:
391 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
392 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
393 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
394 ;;; End:
396 ;;; arch-tag: 9b798cf9-02ba-487f-a62e-b63aa823dbfb
397 ;;; ediff-merg.el ends here