1 ;;; ediff-merg.el --- merging utilities
3 ;; Copyright (C) 1994, 1995, 1996, 1997 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)
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.
29 (defvar ediff-window-A
)
30 (defvar ediff-window-B
)
31 (defvar ediff-window-C
)
32 (defvar ediff-merge-window-share
)
33 (defvar ediff-window-config-saved
)
36 (let ((load-path (cons (expand-file-name ".") load-path
)))
37 (or (featurep 'ediff-init
)
38 (load "ediff-init.el" nil nil
'nosuffix
))
39 (or (featurep 'ediff-util
)
40 (load "ediff-util.el" nil nil
'nosuffix
))
46 (defcustom ediff-quit-merge-hook
'ediff-maybe-save-and-delete-merge
47 "*Hooks to run before quitting a merge job.
48 The most common use is to save and delete the merge buffer."
53 (defcustom ediff-default-variant
'combined
54 "*The variant to be used as a default for buffer C in merging.
55 Valid values are the symbols `default-A', `default-B', and `combined'."
56 :type
'(radio (const default-A
) (const default-B
) (const combined
))
59 (defcustom ediff-combination-pattern
60 '("<<<<<<< variant A" A
">>>>>>> variant B" B
"####### Ancestor" Ancestor
"======= end")
61 "*Pattern to be used for combining difference regions in buffers A and B.
62 The value must be a list of the form
63 (STRING1 bufspec1 STRING2 bufspec2 STRING3 bufspec3 STRING4)
64 where bufspec is the symbol A, B, or Ancestor. For instance, if the value is
65 '(STRING1 A STRING2 Ancestor STRING3 B STRING4) then the
66 combined text will look like this:
69 diff region from variant A
71 diff region from the ancestor
73 diff region from variant B
76 :type
'(choice (list string symbol string symbol string
)
77 (list string symbol string symbol string symbol string
))
80 (defcustom ediff-show-clashes-only nil
81 "*If t, show only those diff regions where both buffers disagree with the ancestor.
82 This means that regions that have status prefer-A or prefer-B will be
83 skiped over. Nil means show all regions."
87 (make-variable-buffer-local 'ediff-show-clashes-only
)
89 (defcustom ediff-skip-merge-regions-that-differ-from-default nil
90 "*If t, show only the regions that have not been changed by the user.
91 A region is considered to have been changed if it is different from the current
92 default (`default-A', `default-B', `combined') and it hasn't been marked as
93 `prefer-A' or `prefer-B'.
94 A region is considered to have been changed also when it is marked as
95 as `prefer-A', but is different from the corresponding difference region in
96 Buffer A or if it is marked as `prefer-B' and is different from the region in
101 (make-variable-buffer-local 'ediff-skip-merge-regions-that-differ-from-default
)
103 ;; If ediff-show-clashes-only, check if there is no clash between the ancestor
104 ;; and one of the variants.
105 (defsubst ediff-merge-region-is-non-clash
(n)
106 (and ediff-show-clashes-only
107 (string-match "prefer" (or (ediff-get-state-of-merge n
) ""))))
109 ;; If ediff-skip-changed-regions, check if the merge region differs from
110 ;; the current default. If a region is different from the default, it means
111 ;; that the user has made determination as to how to merge for this particular
113 (defsubst ediff-skip-merge-region-if-changed-from-default-p
(n)
114 (and ediff-skip-merge-regions-that-differ-from-default
115 (ediff-merge-changed-from-default-p n
'prefers-too
)))
118 (defun ediff-get-combined-region (n)
119 (let ((pattern-list ediff-combination-pattern
)
122 "ediff-combination-pattern: Invalid format. Please consult the documentation")
123 diff-region region-delim region-spec
)
125 (if (< (length pattern-list
) 5)
128 (while (> (length pattern-list
) 2)
129 (setq region-delim
(nth 0 pattern-list
)
130 region-spec
(nth 1 pattern-list
))
131 (or (and (stringp region-delim
) (memq region-spec
'(A B Ancestor
)))
138 (ediff-get-region-contents
139 n region-spec ediff-control-buffer
)))
141 (setq pattern-list
(cdr (cdr pattern-list
)))
144 (setq region-delim
(nth 0 pattern-list
))
145 (or (stringp region-delim
)
147 (setq combo-region
(concat combo-region region-delim
"\n"))
150 ;;(defsubst ediff-make-combined-diff (regA regB)
151 ;; (concat (nth 0 ediff-combination-pattern) "\n"
153 ;; (nth 1 ediff-combination-pattern) "\n"
155 ;; (nth 2 ediff-combination-pattern) "\n"))
157 (defsubst ediff-set-state-of-all-diffs-in-all-buffers
(ctl-buf)
159 (while (< n ediff-number-of-differences
)
160 (ediff-set-state-of-diff-in-all-buffers n ctl-buf
)
163 (defun ediff-set-state-of-diff-in-all-buffers (n ctl-buf
)
164 (let ((regA (ediff-get-region-contents n
'A ctl-buf
))
165 (regB (ediff-get-region-contents n
'B ctl-buf
))
166 (regC (ediff-get-region-contents n
'C ctl-buf
)))
167 (cond ((and (string= regA regB
) (string= regA regC
))
168 (ediff-set-state-of-diff n
'A
"=diff(B)")
169 (ediff-set-state-of-diff n
'B
"=diff(C)")
170 (ediff-set-state-of-diff n
'C
"=diff(A)"))
172 (ediff-set-state-of-diff n
'A
"=diff(B)")
173 (ediff-set-state-of-diff n
'B
"=diff(A)")
174 (ediff-set-state-of-diff n
'C nil
))
176 (ediff-set-state-of-diff n
'A
"=diff(C)")
177 (ediff-set-state-of-diff n
'C
"=diff(A)")
178 (ediff-set-state-of-diff n
'B nil
))
180 (ediff-set-state-of-diff n
'C
"=diff(B)")
181 (ediff-set-state-of-diff n
'B
"=diff(C)")
182 (ediff-set-state-of-diff n
'A nil
))
183 ((string= regC
(ediff-get-combined-region n
))
184 (ediff-set-state-of-diff n
'A nil
)
185 (ediff-set-state-of-diff n
'B nil
)
186 (ediff-set-state-of-diff n
'C
"=diff(A+B)"))
187 (t (ediff-set-state-of-diff n
'A nil
)
188 (ediff-set-state-of-diff n
'B nil
)
189 (ediff-set-state-of-diff n
'C nil
)))
192 (defun ediff-set-merge-mode ()
194 (remove-hook 'local-write-file-hooks
'ediff-set-merge-mode
))
197 ;; Go over all diffs starting with DIFF-NUM and copy regions into buffer C
198 ;; according to the state of the difference.
199 ;; Since ediff-copy-diff refuses to copy identical diff regions, there is
200 ;; no need to optimize ediff-do-merge any further.
202 ;; If re-merging, change state of merge in all diffs starting with
203 ;; DIFF-NUM, except those where the state is prefer-* or where it is
204 ;; `default-*' or `combined' but the buf C region appears to be modified
205 ;; since last set by default.
206 (defun ediff-do-merge (diff-num &optional remerging
)
207 (if (< diff-num
0) (setq diff-num
0))
209 ;;(default-state-of-merge (format "%S" ediff-default-variant))
210 do-not-copy state-of-merge
)
211 (while (< n ediff-number-of-differences
)
212 (setq do-not-copy nil
) ; reset after each cycle
214 (message "%s buffers A & B into C ... region %d of %d"
215 (if remerging
"Re-merging" "Merging")
217 ediff-number-of-differences
))
219 (setq state-of-merge
(ediff-get-state-of-merge n
))
222 (let ((reg-A (ediff-get-region-contents n
'A ediff-control-buffer
))
223 (reg-B (ediff-get-region-contents n
'B ediff-control-buffer
))
224 (reg-C (ediff-get-region-contents n
'C ediff-control-buffer
)))
226 ;; if region was edited since it was first set by default
227 (if (or (ediff-merge-changed-from-default-p n
)
229 (string-match "prefer" state-of-merge
))
231 (setq do-not-copy t
))
233 ;; change state of merge for this diff, if necessary
234 (if (and (string-match "\\(default\\|combined\\)" state-of-merge
)
236 (ediff-set-state-of-merge
237 n
(format "%S" ediff-default-variant
)))
240 ;; state-of-merge may have changed via ediff-set-state-of-merge, so
241 ;; check it once again
242 (setq state-of-merge
(ediff-get-state-of-merge n
))
245 (if (string= state-of-merge
"combined")
246 ;; use n+1 because ediff-combine-diffs works via user numbering
247 ;; of diffs, which is 1+ to what ediff uses internally
248 (ediff-combine-diffs (1+ n
) 'batch
)
250 n
(if (string-match "-A" state-of-merge
) 'A
'B
) 'C
'batch
)))
252 (message "Merging buffers A & B into C ... Done")
256 (defun ediff-re-merge ()
257 "Remerge unmodified diff regions using a new default. Start with the current region."
259 (let* ((default-variant-alist
260 (list '("default-A") '("default-B") '("combined")))
262 (delete (list (symbol-name ediff-default-variant
))
263 default-variant-alist
)))
264 (setq ediff-default-variant
267 (format "Current merge default is `%S'. New default: "
268 ediff-default-variant
)
269 actual-alist nil
'must-match
)))
270 (ediff-do-merge ediff-current-difference
'remerge
)
274 (defun ediff-shrink-window-C (arg)
275 "Shrink window C to just one line.
276 With a prefix argument, returns window C to its normal size.
277 Used only for merging jobs."
279 (if (not ediff-merge-job
)
280 (error "ediff-shrink-window-C can be used only for merging jobs"))
281 (cond ((eq arg
'-
) (setq arg -
1))
282 ((not (numberp arg
)) (setq arg nil
)))
284 (let ((ediff-merge-window-share
285 (if (< (window-height ediff-window-C
) 3)
286 ediff-merge-window-share
0)))
287 (setq ediff-window-config-saved
"") ; force redisplay
288 (ediff-recenter 'no-rehighlight
)))
289 ((and (< arg
0) (> (window-height ediff-window-C
) 2))
290 (setq ediff-merge-window-share
(* ediff-merge-window-share
0.9))
291 (setq ediff-window-config-saved
"") ; force redisplay
292 (ediff-recenter 'no-rehighlight
))
293 ((and (> arg
0) (> (window-height ediff-window-A
) 2))
294 (setq ediff-merge-window-share
(* ediff-merge-window-share
1.1))
295 (setq ediff-window-config-saved
"") ; force redisplay
296 (ediff-recenter 'no-rehighlight
))))
299 ;; N here is the user's region number. It is 1+ what Ediff uses internally.
300 (defun ediff-combine-diffs (n &optional batch-invocation
)
301 "Combine Nth diff regions of buffers A and B and place the combination in C.
302 N is a prefix argument. If nil, combine the current difference regions.
303 Combining is done according to the specifications in variable
304 `ediff-combination-pattern'."
306 (setq n
(if (numberp n
) (1- n
) ediff-current-difference
))
308 (let (regA regB reg-combined
)
309 ;;(setq regA (ediff-get-region-contents n 'A ediff-control-buffer)
310 ;; regB (ediff-get-region-contents n 'B ediff-control-buffer))
311 ;;(setq reg-combined (ediff-make-combined-diff regA regB))
312 (setq reg-combined
(ediff-get-combined-region n
))
314 (ediff-copy-diff n nil
'C batch-invocation reg-combined
))
315 (or batch-invocation
(ediff-jump-to-difference (1+ n
))))
318 ;; Checks if the region in buff C looks like a combination of the regions
319 ;; in buffers A and B. Return a list (reg-a-beg reg-a-end reg-b-beg reg-b-end)
320 ;; These refer to where the delimiters for region A, B, Ancestor start and end
322 (defun ediff-looks-like-combined-merge (region-num)
324 (let ((combined (string-match (regexp-quote "(A+B)")
325 (or (ediff-get-state-of-diff region-num
'C
)
327 (mrgreg-beg (ediff-get-diff-posn 'C
'beg region-num
))
328 (mrgreg-end (ediff-get-diff-posn 'C
'end region-num
))
329 (pattern-list ediff-combination-pattern
)
330 delim reg-beg reg-end delim-regs-list
)
333 (ediff-with-current-buffer ediff-buffer-C
335 (goto-char mrgreg-beg
)
336 (setq delim
(nth 0 pattern-list
))
337 (search-forward delim mrgreg-end
'noerror
)
338 (setq reg-beg
(match-beginning 0))
339 (setq reg-end
(match-end 0))
340 (if (and reg-beg reg-end
)
341 (setq delim-regs-list
343 (cons reg-end
(cons reg-beg delim-regs-list
))))
344 (if (> (length pattern-list
) 1)
345 (setq pattern-list
(cdr (cdr pattern-list
)))
346 (setq pattern-list nil
))
349 (reverse delim-regs-list
)
353 ;; Check if the non-preferred merge has been modified since originally set.
354 ;; This affects only the regions that are marked as default-A/B or combined.
355 ;; If PREFERS-TOO is non-nil, then look at the regions marked as prefers-A/B as
357 (defun ediff-merge-changed-from-default-p (diff-num &optional prefers-too
)
358 (let ((reg-A (ediff-get-region-contents diff-num
'A ediff-control-buffer
))
359 (reg-B (ediff-get-region-contents diff-num
'B ediff-control-buffer
))
360 (reg-C (ediff-get-region-contents diff-num
'C ediff-control-buffer
)))
362 (setq state-of-merge
(ediff-get-state-of-merge diff-num
))
364 ;; if region was edited since it was first set by default
365 (or (and (string= state-of-merge
"default-A")
366 (not (string= reg-A reg-C
)))
367 (and (string= state-of-merge
"default-B")
368 (not (string= reg-B reg-C
)))
369 (and (string= state-of-merge
"combined")
370 ;;(not (string= (ediff-make-combined-diff reg-A reg-B) reg-C)))
371 (not (string= (ediff-get-combined-region diff-num
) reg-C
)))
373 (string= state-of-merge
"prefer-A")
374 (not (string= reg-A reg-C
)))
376 (string= state-of-merge
"prefer-B")
377 (not (string= reg-B reg-C
)))
382 ;;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
383 ;;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
384 ;;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
387 ;; ediff-merg.el ends here