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