1 ;;; ediff-merg.el --- merging utilities
3 ;; Copyright (C) 1994-2013 Free Software Foundation, Inc.
5 ;; 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 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
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
)
38 (defcustom ediff-quit-merge-hook
'ediff-maybe-save-and-delete-merge
39 "Hooks to run before quitting a merge job.
40 The most common use is to save and delete the merge buffer."
45 (defcustom ediff-default-variant
'combined
46 "The variant to be used as a default for buffer C in merging.
47 Valid values are the symbols `default-A', `default-B', and `combined'."
48 :type
'(radio (const default-A
) (const default-B
) (const combined
))
51 (defcustom ediff-combination-pattern
52 '("<<<<<<< variant A" A
">>>>>>> variant B" B
"####### Ancestor" Ancestor
"======= end")
53 "Pattern to be used for combining difference regions in buffers A and B.
54 The value must be a list of the form
55 \(STRING1 bufspec1 STRING2 bufspec2 STRING3 bufspec3 STRING4)
56 where bufspec is the symbol A, B, or Ancestor. For instance, if the value is
57 '(STRING1 A STRING2 Ancestor STRING3 B STRING4) then the
58 combined text will look like this:
61 diff region from variant A
63 diff region from the ancestor
65 diff region from variant B
68 :type
'(choice (list string symbol string symbol string
)
69 (list string symbol string symbol string symbol string
))
72 (defcustom ediff-show-clashes-only nil
73 "If t, show only those diff regions where both buffers disagree with the ancestor.
74 This means that regions that have status prefer-A or prefer-B will be
75 skipped over. A value of nil means show all regions."
79 (make-variable-buffer-local 'ediff-show-clashes-only
)
81 (defcustom ediff-skip-merge-regions-that-differ-from-default nil
82 "If t, show only the regions that have not been changed by the user.
83 A region is considered to have been changed if it is different from the current
84 default (`default-A', `default-B', `combined') and it hasn't been marked as
85 `prefer-A' or `prefer-B'.
86 A region is considered to have been changed also when it is marked as
87 as `prefer-A', but is different from the corresponding difference region in
88 Buffer A or if it is marked as `prefer-B' and is different from the region in
93 (make-variable-buffer-local 'ediff-skip-merge-regions-that-differ-from-default
)
95 ;; check if there is no clash between the ancestor and one of the variants.
96 ;; if it is not a merge job then return true
97 (defun ediff-merge-region-is-non-clash (n)
99 (string-match "prefer" (or (ediff-get-state-of-merge n
) ""))
102 ;; If ediff-show-clashes-only, check if there is no clash between the ancestor
103 ;; and one of the variants.
104 (defun ediff-merge-region-is-non-clash-to-skip (n)
105 (and (ediff-merge-job)
106 ediff-show-clashes-only
107 (ediff-merge-region-is-non-clash 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 (defun ediff-skip-merge-region-if-changed-from-default-p (n)
114 (and (ediff-merge-job)
115 ediff-skip-merge-regions-that-differ-from-default
116 (ediff-merge-changed-from-default-p n
'prefers-too
)))
119 (defun ediff-get-combined-region (n)
120 (let ((pattern-list ediff-combination-pattern
)
123 "ediff-combination-pattern: Invalid format. Please consult the documentation")
124 region-delim region-spec
)
126 (if (< (length pattern-list
) 5)
129 (while (> (length pattern-list
) 2)
130 (setq region-delim
(nth 0 pattern-list
)
131 region-spec
(nth 1 pattern-list
))
132 (or (and (stringp region-delim
) (memq region-spec
'(A B Ancestor
)))
139 (ediff-get-region-contents
140 n region-spec ediff-control-buffer
)))
142 (setq pattern-list
(cdr (cdr pattern-list
)))
145 (setq region-delim
(nth 0 pattern-list
))
146 (or (stringp region-delim
)
148 (setq combo-region
(concat combo-region region-delim
"\n"))
151 ;;(defsubst ediff-make-combined-diff (regA regB)
152 ;; (concat (nth 0 ediff-combination-pattern) "\n"
154 ;; (nth 1 ediff-combination-pattern) "\n"
156 ;; (nth 2 ediff-combination-pattern) "\n"))
158 (defsubst ediff-set-state-of-all-diffs-in-all-buffers
(ctl-buf)
160 (while (< n ediff-number-of-differences
)
161 (ediff-set-state-of-diff-in-all-buffers n ctl-buf
)
164 (defun ediff-set-state-of-diff-in-all-buffers (n ctl-buf
)
165 (let ((regA (ediff-get-region-contents n
'A ctl-buf
))
166 (regB (ediff-get-region-contents n
'B ctl-buf
))
167 (regC (ediff-get-region-contents n
'C ctl-buf
)))
168 (cond ((and (string= regA regB
) (string= regA regC
))
169 (ediff-set-state-of-diff n
'A
"=diff(B)")
170 (ediff-set-state-of-diff n
'B
"=diff(C)")
171 (ediff-set-state-of-diff n
'C
"=diff(A)"))
173 (ediff-set-state-of-diff n
'A
"=diff(B)")
174 (ediff-set-state-of-diff n
'B
"=diff(A)")
175 (ediff-set-state-of-diff n
'C nil
))
177 (ediff-set-state-of-diff n
'A
"=diff(C)")
178 (ediff-set-state-of-diff n
'C
"=diff(A)")
179 (ediff-set-state-of-diff n
'B nil
))
181 (ediff-set-state-of-diff n
'C
"=diff(B)")
182 (ediff-set-state-of-diff n
'B
"=diff(C)")
183 (ediff-set-state-of-diff n
'A nil
))
184 ((string= regC
(ediff-get-combined-region n
))
185 (ediff-set-state-of-diff n
'A nil
)
186 (ediff-set-state-of-diff n
'B nil
)
187 (ediff-set-state-of-diff n
'C
"=diff(A+B)"))
188 (t (ediff-set-state-of-diff n
'A nil
)
189 (ediff-set-state-of-diff n
'B nil
)
190 (ediff-set-state-of-diff n
'C nil
)))
193 (defun ediff-set-merge-mode ()
195 (remove-hook 'local-write-file-hooks
'ediff-set-merge-mode
))
198 ;; Go over all diffs starting with DIFF-NUM and copy regions into buffer C
199 ;; according to the state of the difference.
200 ;; Since ediff-copy-diff refuses to copy identical diff regions, there is
201 ;; no need to optimize ediff-do-merge any further.
203 ;; If re-merging, change state of merge in all diffs starting with
204 ;; DIFF-NUM, except those where the state is prefer-* or where it is
205 ;; `default-*' or `combined' but the buf C region appears to be modified
206 ;; since last set by default.
207 (defun ediff-do-merge (diff-num &optional remerging
)
208 (if (< diff-num
0) (setq diff-num
0))
210 ;;(default-state-of-merge (format "%S" ediff-default-variant))
211 do-not-copy state-of-merge
)
212 (while (< n ediff-number-of-differences
)
213 (setq do-not-copy nil
) ; reset after each cycle
215 (message "%s buffers A & B into C ... region %d of %d"
216 (if remerging
"Re-merging" "Merging")
218 ediff-number-of-differences
))
220 (setq state-of-merge
(ediff-get-state-of-merge n
))
223 ;;(let ((reg-A (ediff-get-region-contents n 'A ediff-control-buffer))
224 ;; (reg-B (ediff-get-region-contents n 'B ediff-control-buffer))
225 ;; (reg-C (ediff-get-region-contents n 'C ediff-control-buffer)))
228 ;; if region was edited since it was first set by default
229 (if (or (ediff-merge-changed-from-default-p n
)
231 (string-match "prefer" state-of-merge
))
233 (setq do-not-copy t
))
235 ;; change state of merge for this diff, if necessary
236 (if (and (string-match "\\(default\\|combined\\)" state-of-merge
)
238 (ediff-set-state-of-merge
239 n
(format "%S" ediff-default-variant
)))
242 ;; state-of-merge may have changed via ediff-set-state-of-merge, so
243 ;; check it once again
244 (setq state-of-merge
(ediff-get-state-of-merge n
))
247 (if (string= state-of-merge
"combined")
248 ;; use n+1 because ediff-combine-diffs works via user numbering
249 ;; of diffs, which is 1+ to what ediff uses internally
250 (ediff-combine-diffs (1+ n
) 'batch
)
252 n
(if (string-match "-A" state-of-merge
) 'A
'B
) 'C
'batch
)))
254 (message "Merging buffers A & B into C ... Done")
258 (defun ediff-re-merge ()
259 "Remerge unmodified diff regions using a new default. Start with the current region."
261 (let* ((default-variant-alist
262 (list '("default-A") '("default-B") '("combined")))
264 (delete (list (symbol-name ediff-default-variant
))
265 default-variant-alist
)))
266 (setq ediff-default-variant
269 (format "Current merge default is `%S'. New default: "
270 ediff-default-variant
)
271 actual-alist nil
'must-match
)))
272 (ediff-do-merge ediff-current-difference
'remerge
)
276 (defun ediff-shrink-window-C (arg)
277 "Shrink window C to just one line.
278 With a prefix argument, returns window C to its normal size.
279 Used only for merging jobs."
281 (if (not ediff-merge-job
)
282 (error "ediff-shrink-window-C can be used only for merging jobs"))
283 (cond ((eq arg
'-
) (setq arg -
1))
284 ((not (numberp arg
)) (setq arg nil
)))
286 (let ((ediff-merge-window-share
287 (if (< (window-height ediff-window-C
) 3)
288 ediff-merge-window-share
0)))
289 (setq ediff-window-config-saved
"") ; force redisplay
290 (ediff-recenter 'no-rehighlight
)))
291 ((and (< arg
0) (> (window-height ediff-window-C
) 2))
292 (setq ediff-merge-window-share
(* ediff-merge-window-share
0.9))
293 (setq ediff-window-config-saved
"") ; force redisplay
294 (ediff-recenter 'no-rehighlight
))
295 ((and (> arg
0) (> (window-height ediff-window-A
) 2))
296 (setq ediff-merge-window-share
(* ediff-merge-window-share
1.1))
297 (setq ediff-window-config-saved
"") ; force redisplay
298 (ediff-recenter 'no-rehighlight
))))
301 ;; N here is the user's region number. It is 1+ what Ediff uses internally.
302 (defun ediff-combine-diffs (n &optional batch-invocation
)
303 "Combine Nth diff regions of buffers A and B and place the combination in C.
304 N is a prefix argument. If nil, combine the current difference regions.
305 Combining is done according to the specifications in variable
306 `ediff-combination-pattern'."
308 (setq n
(if (numberp n
) (1- n
) ediff-current-difference
))
311 ;;(setq regA (ediff-get-region-contents n 'A ediff-control-buffer)
312 ;; regB (ediff-get-region-contents n 'B ediff-control-buffer))
313 ;;(setq reg-combined (ediff-make-combined-diff regA regB))
314 (setq reg-combined
(ediff-get-combined-region n
))
316 (ediff-copy-diff n nil
'C batch-invocation reg-combined
))
317 (or batch-invocation
(ediff-jump-to-difference (1+ n
))))
320 ;; Checks if the region in buff C looks like a combination of the regions
321 ;; in buffers A and B. Return a list (reg-a-beg reg-a-end reg-b-beg reg-b-end)
322 ;; These refer to where the delimiters for region A, B, Ancestor start and end
324 (defun ediff-looks-like-combined-merge (region-num)
326 (let ((combined (string-match (regexp-quote "(A+B)")
327 (or (ediff-get-state-of-diff region-num
'C
)
329 (mrgreg-beg (ediff-get-diff-posn 'C
'beg region-num
))
330 (mrgreg-end (ediff-get-diff-posn 'C
'end region-num
))
331 (pattern-list ediff-combination-pattern
)
332 delim reg-beg reg-end delim-regs-list
)
335 (ediff-with-current-buffer ediff-buffer-C
337 (goto-char mrgreg-beg
)
338 (setq delim
(nth 0 pattern-list
))
339 (search-forward delim mrgreg-end
'noerror
)
340 (setq reg-beg
(match-beginning 0))
341 (setq reg-end
(match-end 0))
342 (if (and reg-beg reg-end
)
343 (setq delim-regs-list
345 (cons reg-end
(cons reg-beg delim-regs-list
))))
346 (if (> (length pattern-list
) 1)
347 (setq pattern-list
(cdr (cdr pattern-list
)))
348 (setq pattern-list nil
))
351 (reverse delim-regs-list
)
354 (defvar state-of-merge
) ; dynamic var
356 ;; Check if the non-preferred merge has been modified since originally set.
357 ;; This affects only the regions that are marked as default-A/B or combined.
358 ;; If PREFERS-TOO is non-nil, then look at the regions marked as prefers-A/B as
360 (defun ediff-merge-changed-from-default-p (diff-num &optional prefers-too
)
361 (let ((reg-A (ediff-get-region-contents diff-num
'A ediff-control-buffer
))
362 (reg-B (ediff-get-region-contents diff-num
'B ediff-control-buffer
))
363 (reg-C (ediff-get-region-contents diff-num
'C ediff-control-buffer
)))
365 (setq state-of-merge
(ediff-get-state-of-merge diff-num
))
367 ;; if region was edited since it was first set by default
368 (or (and (string= state-of-merge
"default-A")
369 (not (string= reg-A reg-C
)))
370 (and (string= state-of-merge
"default-B")
371 (not (string= reg-B reg-C
)))
372 (and (string= state-of-merge
"combined")
373 ;;(not (string= (ediff-make-combined-diff reg-A reg-B) reg-C)))
374 (not (string= (ediff-get-combined-region diff-num
) reg-C
)))
376 (string= state-of-merge
"prefer-A")
377 (not (string= reg-A reg-C
)))
379 (string= state-of-merge
"prefer-B")
380 (not (string= reg-B reg-C
)))
384 (provide 'ediff-merg
)
388 ;; eval: (put 'ediff-defvar-local 'lisp-indent-hook 'defun)
389 ;; eval: (put 'ediff-with-current-buffer 'lisp-indent-hook 1)
390 ;; eval: (put 'ediff-with-current-buffer 'edebug-form-spec '(form body))
393 ;;; ediff-merg.el ends here