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