11042
|
1 ;;; ediff-merg.el --- merging utilities
|
|
2 ;;; Copyright (C) 1994 Free Software Foundation, Inc.
|
|
3
|
|
4 ;; Author: Michael Kifer <kifer@cs.sunysb.edu>
|
|
5
|
|
6 ;; This file is part of GNU Emacs.
|
|
7
|
|
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
|
|
9 ;; it under the terms of the GNU General Public License as published by
|
|
10 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
11 ;; any later version.
|
|
12
|
|
13 ;; GNU Emacs is distributed in the hope that it will be useful,
|
|
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 ;; GNU General Public License for more details.
|
|
17
|
|
18 ;; You should have received a copy of the GNU General Public License
|
|
19 ;; along with GNU Emacs; see the file COPYING. If not, write to
|
|
20 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
|
21
|
|
22
|
|
23 (require 'ediff-init)
|
|
24
|
|
25
|
|
26 (defvar ediff-default-variant 'default-A
|
|
27 "*The variant to be used as a default for buffer C in merging.
|
|
28 Valid values are the symbols `default-A', `default-B', and `combined'.")
|
|
29
|
|
30 (defvar ediff-combination-pattern
|
|
31 '("#ifdef NEW /* variant A */" "#else /* variant B */" "#endif /* NEW */")
|
|
32 "*Pattern to be used for combining difference regions in buffers A and B.
|
|
33 The value is (STRING1 STRING2 STRING3). The combined text will look like this:
|
|
34
|
|
35 STRING1
|
|
36 diff region from variant A
|
|
37 STRING2
|
|
38 diff region from variant B
|
|
39 STRING3
|
|
40 ")
|
|
41
|
|
42 (ediff-defvar-local ediff-show-clashes-only nil
|
|
43 "*If t, show only those diff regions where both buffers disagree with the ancestor.
|
|
44 This means that regions that have status prefer-A or prefer-B will be
|
|
45 skiped over. Nil means show all regions.")
|
|
46
|
|
47
|
|
48 (defsubst ediff-get-combined-region (n)
|
|
49 (concat (nth 0 ediff-combination-pattern) "\n"
|
|
50 (ediff-get-region-contents n 'A ediff-control-buffer)
|
|
51 (nth 1 ediff-combination-pattern) "\n"
|
|
52 (ediff-get-region-contents n 'B ediff-control-buffer)
|
|
53 (nth 2 ediff-combination-pattern) "\n"))
|
|
54
|
|
55 (defsubst ediff-set-state-of-all-diffs-in-all-buffers (ctl-buf)
|
|
56 (let ((n 0))
|
|
57 (while (< n ediff-number-of-differences)
|
|
58 (ediff-set-state-of-diff-in-all-buffers n ctl-buf)
|
|
59 (setq n (1+ n)))))
|
|
60
|
|
61 (defun ediff-set-state-of-diff-in-all-buffers (n ctl-buf)
|
|
62 (let ((regA (ediff-get-region-contents n 'A ctl-buf))
|
|
63 (regB (ediff-get-region-contents n 'B ctl-buf))
|
|
64 (regC (ediff-get-region-contents n 'C ctl-buf)))
|
|
65 (cond ((string= regA regB)
|
|
66 (ediff-set-state-of-diff n 'A "=diff(B)")
|
|
67 (ediff-set-state-of-diff n 'B "=diff(A)")
|
|
68 (ediff-set-state-of-diff n 'C nil))
|
|
69 ((string= regA regC)
|
|
70 (ediff-set-state-of-diff n 'A "=diff(C)")
|
|
71 (ediff-set-state-of-diff n 'C "=diff(A)")
|
|
72 (ediff-set-state-of-diff n 'B nil))
|
|
73 ((string= regB regC)
|
|
74 (ediff-set-state-of-diff n 'C "=diff(B)")
|
|
75 (ediff-set-state-of-diff n 'B "=diff(C)")
|
|
76 (ediff-set-state-of-diff n 'A nil))
|
|
77 ((string= regC (ediff-get-combined-region n))
|
|
78 (ediff-set-state-of-diff n 'A nil)
|
|
79 (ediff-set-state-of-diff n 'B nil)
|
|
80 (ediff-set-state-of-diff n 'C "=diff(A+B)"))
|
|
81 (t (ediff-set-state-of-diff n 'A nil)
|
|
82 (ediff-set-state-of-diff n 'B nil)
|
|
83 (ediff-set-state-of-diff n 'C nil)))
|
|
84 ))
|
|
85
|
|
86 ;; Go over all diffs starting with DIFF-NUM and copy regions into buffer C
|
|
87 ;; according to the state of the difference.
|
|
88 ;; Since ediff-copy-diff refuses to copy identical diff regions, there is
|
|
89 ;; no need to optimize ediff-do-merge any further.
|
|
90 ;;
|
|
91 ;; If re-merging, change state of merge in all diffs starting with
|
|
92 ;; DIFF-NUM, except those where the state is prefer-* or where it is
|
|
93 ;; default-* but the region appears to be modified with respect to its
|
|
94 ;; original form.
|
|
95 ;; Also, prevent copying when there is a danger of overriding a modified
|
|
96 ;; region in buffer C.
|
|
97 (defun ediff-do-merge (diff-num &optional remerging)
|
|
98 (if (< diff-num 0) (setq diff-num 0))
|
|
99 (let ((n diff-num)
|
|
100 (default-state-of-merge (format "%S" ediff-default-variant))
|
|
101 do-not-copy
|
|
102 state-of-merge)
|
|
103
|
|
104 (while (< n ediff-number-of-differences)
|
|
105
|
|
106 (if (= (mod n 10) 0)
|
|
107 (message "%s buffers A & B into C ... region %d of %d"
|
|
108 (if remerging "Re-merging" "Merging")
|
|
109 n
|
|
110 ediff-number-of-differences))
|
|
111
|
|
112 (setq state-of-merge (ediff-get-state-of-merge n))
|
|
113 (setq do-not-copy (string= state-of-merge default-state-of-merge))
|
|
114
|
|
115 (if remerging
|
|
116 (let ((reg-A (ediff-get-region-contents n 'A ediff-control-buffer))
|
|
117 (reg-B (ediff-get-region-contents n 'B ediff-control-buffer))
|
|
118 (reg-C (ediff-get-region-contents n 'C ediff-control-buffer)))
|
|
119
|
|
120 (if (or
|
|
121 ;; has been edited since it was first set by default
|
|
122 (and (string= state-of-merge "default-A")
|
|
123 (not (string= reg-A reg-C)))
|
|
124 ;; has been edited since first set by default
|
|
125 (and (string= state-of-merge "default-B")
|
|
126 (not (string= reg-B reg-C)))
|
|
127 ;; was prefered--ignore
|
|
128 (string-match "prefer" state-of-merge)
|
|
129 ;; was combined--ignore
|
|
130 (string= state-of-merge "combined"))
|
|
131
|
|
132 (setq do-not-copy t))
|
|
133
|
|
134 ;; change state of merge for this diff, if necessary
|
|
135 (if (and (string-match "default" state-of-merge) (not do-not-copy))
|
|
136 (ediff-set-state-of-merge
|
|
137 n (format "%S" ediff-default-variant)))
|
|
138 ))
|
|
139
|
|
140 ;; state-of-merge may have changed via ediff-set-state-of-merge, so
|
|
141 ;; check it out again
|
|
142 (setq state-of-merge (ediff-get-state-of-merge n))
|
|
143
|
|
144 (or do-not-copy
|
|
145 (if (string= state-of-merge "combined")
|
|
146 ;; use n+1 because ediff-combine-diffs works via user numbering
|
|
147 ;; of diffs, which is 1+ to what ediff uses internally
|
|
148 (ediff-combine-diffs (1+ n) 'batch)
|
|
149 (ediff-copy-diff n
|
|
150 (if (string-match "-A" state-of-merge) 'A 'B)
|
|
151 'C
|
|
152 'batch)))
|
|
153 (setq n (1+ n)))
|
|
154 (message "Merging buffers A & B into C ... Done")
|
|
155 ))
|
|
156
|
|
157
|
|
158 (defun ediff-re-merge ()
|
|
159 "Remerge unmodified diff regions using a new default. Start with the current region."
|
|
160 (interactive)
|
|
161 (let* ((default-variant-alist
|
|
162 (list '("default-A") '("default-B") '("combined")))
|
|
163 (actual-alist
|
|
164 (delete (list (symbol-name ediff-default-variant))
|
|
165 default-variant-alist)))
|
|
166 (setq ediff-default-variant
|
|
167 (intern
|
|
168 (completing-read
|
|
169 (format "Current merge default is `%S'. New default: "
|
|
170 ediff-default-variant)
|
|
171 actual-alist nil 'must-match)))
|
|
172 (ediff-do-merge ediff-current-difference 'remerge)
|
|
173 (ediff-recenter)
|
|
174 ))
|
|
175
|
|
176 (defun ediff-shrink-window-C (arg)
|
|
177 "Shrink window C to just one line.
|
|
178 With a prefix argument, returns window C to its normal size.
|
|
179 Used only for merging jobs."
|
|
180 (interactive "P")
|
|
181 (if (not ediff-merge-job)
|
|
182 (error "ediff-shrink-window-C can be used only for merging jobs"))
|
|
183 (cond ((eq arg '-) (setq arg -1))
|
|
184 ((not (numberp arg)) (setq arg nil)))
|
|
185 (cond ((null arg)
|
|
186 (let ((ediff-merge-window-share
|
|
187 (if (< (window-height ediff-window-C) 3)
|
|
188 ediff-merge-window-share 0)))
|
|
189 (setq ediff-window-config-saved "") ; force redisplay
|
|
190 (ediff-recenter 'no-rehighlight)))
|
|
191 ((and (< arg 0) (> (window-height ediff-window-C) 2))
|
|
192 (setq ediff-merge-window-share (* ediff-merge-window-share 0.9))
|
|
193 (setq ediff-window-config-saved "") ; force redisplay
|
|
194 (ediff-recenter 'no-rehighlight))
|
|
195 ((and (> arg 0) (> (window-height ediff-window-A) 2))
|
|
196 (setq ediff-merge-window-share (* ediff-merge-window-share 1.1))
|
|
197 (setq ediff-window-config-saved "") ; force redisplay
|
|
198 (ediff-recenter 'no-rehighlight))))
|
|
199
|
|
200
|
|
201 ;; N here is the user's region number. It is 1+ what Ediff uses internally.
|
|
202 (defun ediff-combine-diffs (n &optional batch-invocation)
|
|
203 "Combine Nth diff regions of buffers A and B and place the combination in C.
|
|
204 Combining is done using the list in variable `ediff-combination-pattern'."
|
|
205 (interactive "P")
|
|
206 (setq n (if n (1- n) ediff-current-difference))
|
|
207
|
|
208 (let (regA regB reg-combined)
|
|
209 (setq regA (ediff-get-region-contents n 'A ediff-control-buffer)
|
|
210 regB (ediff-get-region-contents n 'B ediff-control-buffer))
|
|
211
|
|
212 (setq reg-combined (concat (nth 0 ediff-combination-pattern) "\n"
|
|
213 regA
|
|
214 (nth 1 ediff-combination-pattern) "\n"
|
|
215 regB
|
|
216 (nth 2 ediff-combination-pattern) "\n"))
|
|
217
|
|
218 (ediff-copy-diff n nil 'C batch-invocation reg-combined))
|
|
219
|
|
220 (or batch-invocation
|
|
221 (ediff-recenter 'no-rehighlight)))
|
|
222
|
|
223
|
|
224 ;; Checks if the region in buff C looks like a combination of the regions
|
|
225 ;; in buffers A and B. Returns a list (reg-a-beg reg-a-end reg-b-beg reg-b-end)
|
|
226 ;; These refer to where the copies of region A and B start and end in buffer C
|
|
227 (defun ediff-looks-like-combined-merge (region-num)
|
|
228 (if ediff-merge-job
|
|
229 (let ((combined (string-match (regexp-quote "(A+B)")
|
|
230 (or (ediff-get-state-of-diff region-num 'C)
|
|
231 "")))
|
|
232 (reg-beg (ediff-get-diff-posn 'C 'beg region-num))
|
|
233 (reg-end (ediff-get-diff-posn 'C 'end region-num))
|
|
234 (pat1 (nth 0 ediff-combination-pattern))
|
|
235 (pat2 (nth 1 ediff-combination-pattern))
|
|
236 (pat3 (nth 2 ediff-combination-pattern))
|
|
237 reg-a-beg reg-a-end reg-b-beg reg-b-end reg-c-beg reg-c-end)
|
|
238
|
|
239 (if combined
|
|
240 (ediff-eval-in-buffer ediff-buffer-C
|
|
241 (goto-char reg-beg)
|
|
242 (search-forward pat1 reg-end 'noerror)
|
|
243 (setq reg-a-beg (match-beginning 0))
|
|
244 (setq reg-a-end (match-end 0))
|
|
245 (search-forward pat2 reg-end 'noerror)
|
|
246 (setq reg-b-beg (match-beginning 0))
|
|
247 (setq reg-b-end (match-end 0))
|
|
248 (search-forward pat3 reg-end 'noerror)
|
|
249 (setq reg-c-beg (match-beginning 0))
|
|
250 (setq reg-c-end (match-end 0))))
|
|
251
|
|
252 (if (and reg-a-beg reg-a-end reg-b-beg reg-b-end)
|
|
253 (list reg-a-beg reg-a-end reg-b-beg reg-b-end reg-c-beg reg-c-end))
|
|
254 )))
|
|
255
|
|
256
|
|
257 (provide 'ediff-merg)
|
|
258
|
|
259 ;; ediff-merg.el ends here
|