]> code.delx.au - gnu-emacs/blob - lisp/vc/smerge-mode.el
Nuke arch-tags.
[gnu-emacs] / lisp / vc / smerge-mode.el
1 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
2
3 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009, 2010, 2011 Free Software Foundation, Inc.
5
6 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
7 ;; Keywords: vc, tools, revision control, merge, diff3, cvs, conflict
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software: you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; Provides a lightweight alternative to emerge/ediff.
27 ;; To use it, simply add to your .emacs the following lines:
28 ;;
29 ;; (autoload 'smerge-mode "smerge-mode" nil t)
30 ;;
31 ;; you can even have it turned on automatically with the following
32 ;; piece of code in your .emacs:
33 ;;
34 ;; (defun sm-try-smerge ()
35 ;; (save-excursion
36 ;; (goto-char (point-min))
37 ;; (when (re-search-forward "^<<<<<<< " nil t)
38 ;; (smerge-mode 1))))
39 ;; (add-hook 'find-file-hook 'sm-try-smerge t)
40
41 ;;; Todo:
42
43 ;; - if requested, ask the user whether he wants to call ediff right away
44
45 ;;; Code:
46
47 (eval-when-compile (require 'cl))
48 (require 'diff-mode) ;For diff-auto-refine-mode.
49 (require 'newcomment)
50
51 ;;; The real definition comes later.
52 (defvar smerge-mode)
53
54 (defgroup smerge ()
55 "Minor mode to highlight and resolve diff3 conflicts."
56 :group 'tools
57 :prefix "smerge-")
58
59 (defcustom smerge-diff-buffer-name "*vc-diff*"
60 "Buffer name to use for displaying diffs."
61 :group 'smerge
62 :type '(choice
63 (const "*vc-diff*")
64 (const "*cvs-diff*")
65 (const "*smerge-diff*")
66 string))
67
68 (defcustom smerge-diff-switches
69 (append '("-d" "-b")
70 (if (listp diff-switches) diff-switches (list diff-switches)))
71 "A list of strings specifying switches to be passed to diff.
72 Used in `smerge-diff-base-mine' and related functions."
73 :group 'smerge
74 :type '(repeat string))
75
76 (defcustom smerge-auto-leave t
77 "Non-nil means to leave `smerge-mode' when the last conflict is resolved."
78 :group 'smerge
79 :type 'boolean)
80
81 (defface smerge-mine
82 '((((min-colors 88) (background light))
83 (:foreground "blue1"))
84 (((background light))
85 (:foreground "blue"))
86 (((min-colors 88) (background dark))
87 (:foreground "cyan1"))
88 (((background dark))
89 (:foreground "cyan")))
90 "Face for your code."
91 :group 'smerge)
92 (define-obsolete-face-alias 'smerge-mine-face 'smerge-mine "22.1")
93 (defvar smerge-mine-face 'smerge-mine)
94
95 (defface smerge-other
96 '((((background light))
97 (:foreground "darkgreen"))
98 (((background dark))
99 (:foreground "lightgreen")))
100 "Face for the other code."
101 :group 'smerge)
102 (define-obsolete-face-alias 'smerge-other-face 'smerge-other "22.1")
103 (defvar smerge-other-face 'smerge-other)
104
105 (defface smerge-base
106 '((((min-colors 88) (background light))
107 (:foreground "red1"))
108 (((background light))
109 (:foreground "red"))
110 (((background dark))
111 (:foreground "orange")))
112 "Face for the base code."
113 :group 'smerge)
114 (define-obsolete-face-alias 'smerge-base-face 'smerge-base "22.1")
115 (defvar smerge-base-face 'smerge-base)
116
117 (defface smerge-markers
118 '((((background light))
119 (:background "grey85"))
120 (((background dark))
121 (:background "grey30")))
122 "Face for the conflict markers."
123 :group 'smerge)
124 (define-obsolete-face-alias 'smerge-markers-face 'smerge-markers "22.1")
125 (defvar smerge-markers-face 'smerge-markers)
126
127 (defface smerge-refined-change
128 '((t :background "yellow"))
129 "Face used for char-based changes shown by `smerge-refine'."
130 :group 'smerge)
131
132 (easy-mmode-defmap smerge-basic-map
133 `(("n" . smerge-next)
134 ("p" . smerge-prev)
135 ("r" . smerge-resolve)
136 ("a" . smerge-keep-all)
137 ("b" . smerge-keep-base)
138 ("o" . smerge-keep-other)
139 ("m" . smerge-keep-mine)
140 ("E" . smerge-ediff)
141 ("C" . smerge-combine-with-next)
142 ("R" . smerge-refine)
143 ("\C-m" . smerge-keep-current)
144 ("=" . ,(make-sparse-keymap "Diff"))
145 ("=<" "base-mine" . smerge-diff-base-mine)
146 ("=>" "base-other" . smerge-diff-base-other)
147 ("==" "mine-other" . smerge-diff-mine-other))
148 "The base keymap for `smerge-mode'.")
149
150 (defcustom smerge-command-prefix "\C-c^"
151 "Prefix for `smerge-mode' commands."
152 :group 'smerge
153 :type '(choice (const :tag "ESC" "\e")
154 (const :tag "C-c ^" "\C-c^" )
155 (const :tag "none" "")
156 string))
157
158 (easy-mmode-defmap smerge-mode-map
159 `((,smerge-command-prefix . ,smerge-basic-map))
160 "Keymap for `smerge-mode'.")
161
162 (defvar smerge-check-cache nil)
163 (make-variable-buffer-local 'smerge-check-cache)
164 (defun smerge-check (n)
165 (condition-case nil
166 (let ((state (cons (point) (buffer-modified-tick))))
167 (unless (equal (cdr smerge-check-cache) state)
168 (smerge-match-conflict)
169 (setq smerge-check-cache (cons (match-data) state)))
170 (nth (* 2 n) (car smerge-check-cache)))
171 (error nil)))
172
173 (easy-menu-define smerge-mode-menu smerge-mode-map
174 "Menu for `smerge-mode'."
175 '("SMerge"
176 ["Next" smerge-next :help "Go to next conflict"]
177 ["Previous" smerge-prev :help "Go to previous conflict"]
178 "--"
179 ["Keep All" smerge-keep-all :help "Keep all three versions"
180 :active (smerge-check 1)]
181 ["Keep Current" smerge-keep-current :help "Use current (at point) version"
182 :active (and (smerge-check 1) (> (smerge-get-current) 0))]
183 "--"
184 ["Revert to Base" smerge-keep-base :help "Revert to base version"
185 :active (smerge-check 2)]
186 ["Keep Other" smerge-keep-other :help "Keep `other' version"
187 :active (smerge-check 3)]
188 ["Keep Yours" smerge-keep-mine :help "Keep your version"
189 :active (smerge-check 1)]
190 "--"
191 ["Diff Base/Mine" smerge-diff-base-mine
192 :help "Diff `base' and `mine' for current conflict"
193 :active (smerge-check 2)]
194 ["Diff Base/Other" smerge-diff-base-other
195 :help "Diff `base' and `other' for current conflict"
196 :active (smerge-check 2)]
197 ["Diff Mine/Other" smerge-diff-mine-other
198 :help "Diff `mine' and `other' for current conflict"
199 :active (smerge-check 1)]
200 "--"
201 ["Invoke Ediff" smerge-ediff
202 :help "Use Ediff to resolve the conflicts"
203 :active (smerge-check 1)]
204 ["Auto Resolve" smerge-resolve
205 :help "Try auto-resolution heuristics"
206 :active (smerge-check 1)]
207 ["Combine" smerge-combine-with-next
208 :help "Combine current conflict with next"
209 :active (smerge-check 1)]
210 ))
211
212 (easy-menu-define smerge-context-menu nil
213 "Context menu for mine area in `smerge-mode'."
214 '(nil
215 ["Keep Current" smerge-keep-current :help "Use current (at point) version"]
216 ["Kill Current" smerge-kill-current :help "Remove current (at point) version"]
217 ["Keep All" smerge-keep-all :help "Keep all three versions"]
218 "---"
219 ["More..." (popup-menu smerge-mode-menu) :help "Show full SMerge mode menu"]
220 ))
221
222 (defconst smerge-font-lock-keywords
223 '((smerge-find-conflict
224 (1 smerge-mine-face prepend t)
225 (2 smerge-base-face prepend t)
226 (3 smerge-other-face prepend t)
227 ;; FIXME: `keep' doesn't work right with syntactic fontification.
228 (0 smerge-markers-face keep)
229 (4 nil t t)
230 (5 nil t t)))
231 "Font lock patterns for `smerge-mode'.")
232
233 (defconst smerge-begin-re "^<<<<<<< \\(.*\\)\n")
234 (defconst smerge-end-re "^>>>>>>> .*\n")
235 (defconst smerge-base-re "^||||||| .*\n")
236 (defconst smerge-other-re "^=======\n")
237
238 (defvar smerge-conflict-style nil
239 "Keep track of which style of conflict is in use.
240 Can be nil if the style is undecided, or else:
241 - `diff3-E'
242 - `diff3-A'")
243
244 ;; Compiler pacifiers
245 (defvar font-lock-mode)
246 (defvar font-lock-keywords)
247
248 ;;;;
249 ;;;; Actual code
250 ;;;;
251
252 ;; Define smerge-next and smerge-prev
253 (easy-mmode-define-navigation smerge smerge-begin-re "conflict" nil nil
254 (if diff-auto-refine-mode
255 (condition-case nil (smerge-refine) (error nil))))
256
257 (defconst smerge-match-names ["conflict" "mine" "base" "other"])
258
259 (defun smerge-ensure-match (n)
260 (unless (match-end n)
261 (error "No `%s'" (aref smerge-match-names n))))
262
263 (defun smerge-auto-leave ()
264 (when (and smerge-auto-leave
265 (save-excursion (goto-char (point-min))
266 (not (re-search-forward smerge-begin-re nil t))))
267 (when (and (listp buffer-undo-list) smerge-mode)
268 (push (list 'apply 'smerge-mode 1) buffer-undo-list))
269 (smerge-mode -1)))
270
271
272 (defun smerge-keep-all ()
273 "Concatenate all versions."
274 (interactive)
275 (smerge-match-conflict)
276 (let ((mb2 (or (match-beginning 2) (point-max)))
277 (me2 (or (match-end 2) (point-min))))
278 (delete-region (match-end 3) (match-end 0))
279 (delete-region (max me2 (match-end 1)) (match-beginning 3))
280 (if (and (match-end 2) (/= (match-end 1) (match-end 3)))
281 (delete-region (match-end 1) (match-beginning 2)))
282 (delete-region (match-beginning 0) (min (match-beginning 1) mb2))
283 (smerge-auto-leave)))
284
285 (defun smerge-keep-n (n)
286 (smerge-remove-props (match-beginning 0) (match-end 0))
287 ;; We used to use replace-match, but that did not preserve markers so well.
288 (delete-region (match-end n) (match-end 0))
289 (delete-region (match-beginning 0) (match-beginning n)))
290
291 (defun smerge-combine-with-next ()
292 "Combine the current conflict with the next one."
293 ;; `smerge-auto-combine' relies on the finish position (at the beginning
294 ;; of the closing marker).
295 (interactive)
296 (smerge-match-conflict)
297 (let ((ends nil))
298 (dolist (i '(3 2 1 0))
299 (push (if (match-end i) (copy-marker (match-end i) t)) ends))
300 (setq ends (apply 'vector ends))
301 (goto-char (aref ends 0))
302 (if (not (re-search-forward smerge-begin-re nil t))
303 (error "No next conflict")
304 (smerge-match-conflict)
305 (let ((match-data (mapcar (lambda (m) (if m (copy-marker m)))
306 (match-data))))
307 ;; First copy the in-between text in each alternative.
308 (dolist (i '(1 2 3))
309 (when (aref ends i)
310 (goto-char (aref ends i))
311 (insert-buffer-substring (current-buffer)
312 (aref ends 0) (car match-data))))
313 (delete-region (aref ends 0) (car match-data))
314 ;; Then move the second conflict's alternatives into the first.
315 (dolist (i '(1 2 3))
316 (set-match-data match-data)
317 (when (and (aref ends i) (match-end i))
318 (goto-char (aref ends i))
319 (insert-buffer-substring (current-buffer)
320 (match-beginning i) (match-end i))))
321 (delete-region (car match-data) (cadr match-data))
322 ;; Free the markers.
323 (dolist (m match-data) (if m (move-marker m nil)))
324 (mapc (lambda (m) (if m (move-marker m nil))) ends)))))
325
326 (defvar smerge-auto-combine-max-separation 2
327 "Max number of lines between conflicts that should be combined.")
328
329 (defun smerge-auto-combine ()
330 "Automatically combine conflicts that are near each other."
331 (interactive)
332 (save-excursion
333 (goto-char (point-min))
334 (while (smerge-find-conflict)
335 ;; 2 is 1 (default) + 1 (the begin markers).
336 (while (save-excursion
337 (smerge-find-conflict
338 (line-beginning-position
339 (+ 2 smerge-auto-combine-max-separation))))
340 (forward-line -1) ;Go back inside the conflict.
341 (smerge-combine-with-next)
342 (forward-line 1) ;Move past the end of the conflict.
343 ))))
344
345 (defvar smerge-resolve-function
346 (lambda () (error "Don't know how to resolve"))
347 "Mode-specific merge function.
348 The function is called with zero or one argument (non-nil if the resolution
349 function should only apply safe heuristics) and with the match data set
350 according to `smerge-match-conflict'.")
351 (add-to-list 'debug-ignored-errors "Don't know how to resolve")
352
353 (defvar smerge-text-properties
354 `(help-echo "merge conflict: mouse-3 shows a menu"
355 ;; mouse-face highlight
356 keymap (keymap (down-mouse-3 . smerge-popup-context-menu))))
357
358 (defun smerge-remove-props (beg end)
359 (remove-overlays beg end 'smerge 'refine)
360 (remove-overlays beg end 'smerge 'conflict)
361 ;; Now that we use overlays rather than text-properties, this function
362 ;; does not cause refontification any more. It can be seen very clearly
363 ;; in buffers where jit-lock-contextually is not t, in which case deleting
364 ;; the "<<<<<<< foobar" leading line leaves the rest of the conflict
365 ;; highlighted as if it were still a valid conflict. Note that in many
366 ;; important cases (such as the previous example) we're actually called
367 ;; during font-locking so inhibit-modification-hooks is non-nil, so we
368 ;; can't just modify the buffer and expect font-lock to be triggered as in:
369 ;; (put-text-property beg end 'smerge-force-highlighting nil)
370 (with-silent-modifications
371 (remove-text-properties beg end '(fontified nil))))
372
373 (defun smerge-popup-context-menu (event)
374 "Pop up the Smerge mode context menu under mouse."
375 (interactive "e")
376 (if (and smerge-mode
377 (save-excursion (posn-set-point (event-end event)) (smerge-check 1)))
378 (progn
379 (posn-set-point (event-end event))
380 (smerge-match-conflict)
381 (let ((i (smerge-get-current))
382 o)
383 (if (<= i 0)
384 ;; Out of range
385 (popup-menu smerge-mode-menu)
386 ;; Install overlay.
387 (setq o (make-overlay (match-beginning i) (match-end i)))
388 (unwind-protect
389 (progn
390 (overlay-put o 'face 'highlight)
391 (sit-for 0) ;Display the new highlighting.
392 (popup-menu smerge-context-menu))
393 ;; Delete overlay.
394 (delete-overlay o)))))
395 ;; There's no conflict at point, the text-props are just obsolete.
396 (save-excursion
397 (let ((beg (re-search-backward smerge-end-re nil t))
398 (end (re-search-forward smerge-begin-re nil t)))
399 (smerge-remove-props (or beg (point-min)) (or end (point-max)))
400 (push event unread-command-events)))))
401
402 (defun smerge-apply-resolution-patch (buf m0b m0e m3b m3e &optional m2b)
403 "Replace the conflict with a bunch of subconflicts.
404 BUF contains a plain diff between match-1 and match-3."
405 (let ((line 1)
406 (textbuf (current-buffer))
407 (name1 (progn (goto-char m0b)
408 (buffer-substring (+ (point) 8) (line-end-position))))
409 (name2 (when m2b (goto-char m2b) (forward-line -1)
410 (buffer-substring (+ (point) 8) (line-end-position))))
411 (name3 (progn (goto-char m0e) (forward-line -1)
412 (buffer-substring (+ (point) 8) (line-end-position)))))
413 (smerge-remove-props m0b m0e)
414 (delete-region m3e m0e)
415 (delete-region m0b m3b)
416 (setq m3b m0b)
417 (setq m3e (- m3e (- m3b m0b)))
418 (goto-char m3b)
419 (with-current-buffer buf
420 (goto-char (point-min))
421 (while (not (eobp))
422 (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$"))
423 (error "Unexpected patch hunk header: %s"
424 (buffer-substring (point) (line-end-position)))
425 (let* ((op (char-after (match-beginning 3)))
426 (startline (+ (string-to-number (match-string 1))
427 ;; No clue why this is the way it is, but line
428 ;; numbers seem to be off-by-one for `a' ops.
429 (if (eq op ?a) 1 0)))
430 (endline (if (eq op ?a) startline
431 (1+ (if (match-end 2)
432 (string-to-number (match-string 2))
433 startline))))
434 (lines (- endline startline))
435 (otherlines (cond
436 ((eq op ?d) nil)
437 ((null (match-end 5)) 1)
438 (t (- (string-to-number (match-string 5))
439 (string-to-number (match-string 4)) -1))))
440 othertext)
441 (forward-line 1) ;Skip header.
442 (forward-line lines) ;Skip deleted text.
443 (if (eq op ?c) (forward-line 1)) ;Skip separator.
444 (setq othertext
445 (if (null otherlines) ""
446 (let ((pos (point)))
447 (dotimes (i otherlines) (delete-char 2) (forward-line 1))
448 (buffer-substring pos (point)))))
449 (with-current-buffer textbuf
450 (forward-line (- startline line))
451 (insert "<<<<<<< " name1 "\n" othertext
452 (if name2 (concat "||||||| " name2 "\n") "")
453 "=======\n")
454 (forward-line lines)
455 (insert ">>>>>>> " name3 "\n")
456 (setq line endline))))))))
457
458 (defconst smerge-resolve--normalize-re "[\n\t][ \t\n]*\\| [ \t\n]+")
459
460 (defun smerge-resolve--extract-comment (beg end)
461 "Extract the text within the comments that span BEG..END."
462 (save-excursion
463 (let ((comments ())
464 combeg)
465 (goto-char beg)
466 (while (and (< (point) end)
467 (setq combeg (comment-search-forward end t)))
468 (let ((beg (point)))
469 (goto-char combeg)
470 (comment-forward 1)
471 (save-excursion
472 (comment-enter-backward)
473 (push " " comments)
474 (push (buffer-substring-no-properties beg (point)) comments))))
475 (push " " comments)
476 (with-temp-buffer
477 (apply #'insert (nreverse comments))
478 (goto-char (point-min))
479 (while (re-search-forward smerge-resolve--normalize-re
480 nil t)
481 (replace-match " "))
482 (buffer-string)))))
483
484 (defun smerge-resolve--normalize (beg end)
485 (replace-regexp-in-string
486 smerge-resolve--normalize-re " "
487 (concat " " (buffer-substring-no-properties beg end) " ")))
488
489 (defun smerge-resolve (&optional safe)
490 "Resolve the conflict at point intelligently.
491 This relies on mode-specific knowledge and thus only works in some
492 major modes. Uses `smerge-resolve-function' to do the actual work."
493 (interactive)
494 (smerge-match-conflict)
495 (smerge-remove-props (match-beginning 0) (match-end 0))
496 (let ((md (match-data))
497 (m0b (match-beginning 0))
498 (m1b (match-beginning 1))
499 (m2b (match-beginning 2))
500 (m3b (match-beginning 3))
501 (m0e (match-end 0))
502 (m1e (match-end 1))
503 (m2e (match-end 2))
504 (m3e (match-end 3))
505 (buf (generate-new-buffer " *smerge*"))
506 m b o
507 choice)
508 (unwind-protect
509 (progn
510 (cond
511 ;; Trivial diff3 -A non-conflicts.
512 ((and (eq (match-end 1) (match-end 3))
513 (eq (match-beginning 1) (match-beginning 3)))
514 (smerge-keep-n 3))
515 ;; Mode-specific conflict resolution.
516 ((condition-case nil
517 (atomic-change-group
518 (if safe
519 (funcall smerge-resolve-function safe)
520 (funcall smerge-resolve-function))
521 t)
522 (error nil))
523 ;; Nothing to do: the resolution function has done it already.
524 nil)
525 ;; Non-conflict.
526 ((and (eq m1e m3e) (eq m1b m3b))
527 (set-match-data md) (smerge-keep-n 3))
528 ;; Refine a 2-way conflict using "diff -b".
529 ;; In case of a 3-way conflict with an empty base
530 ;; (i.e. 2 conflicting additions), we do the same, presuming
531 ;; that the 2 additions should be somehow merged rather
532 ;; than concatenated.
533 ((let ((lines (count-lines m3b m3e)))
534 (setq m (make-temp-file "smm"))
535 (write-region m1b m1e m nil 'silent)
536 (setq o (make-temp-file "smo"))
537 (write-region m3b m3e o nil 'silent)
538 (not (or (eq m1b m1e) (eq m3b m3e)
539 (and (not (zerop (call-process diff-command
540 nil buf nil "-b" o m)))
541 ;; TODO: We don't know how to do the refinement
542 ;; if there's a non-empty ancestor and m1 and m3
543 ;; aren't just plain equal.
544 m2b (not (eq m2b m2e)))
545 (with-current-buffer buf
546 (goto-char (point-min))
547 ;; Make sure there's some refinement.
548 (looking-at
549 (concat "1," (number-to-string lines) "c"))))))
550 (smerge-apply-resolution-patch buf m0b m0e m3b m3e m2b))
551 ;; "Mere whitespace changes" conflicts.
552 ((when m2e
553 (setq b (make-temp-file "smb"))
554 (write-region m2b m2e b nil 'silent)
555 (with-current-buffer buf (erase-buffer))
556 ;; Only minor whitespace changes made locally.
557 ;; BEWARE: pass "-c" 'cause the output is reused in the next test.
558 (zerop (call-process diff-command nil buf nil "-bc" b m)))
559 (set-match-data md)
560 (smerge-keep-n 3))
561 ;; Try "diff -b BASE MINE | patch OTHER".
562 ((when (and (not safe) m2e b
563 ;; If the BASE is empty, this would just concatenate
564 ;; the two, which is rarely right.
565 (not (eq m2b m2e)))
566 ;; BEWARE: we're using here the patch of the previous test.
567 (with-current-buffer buf
568 (zerop (call-process-region
569 (point-min) (point-max) "patch" t nil nil
570 "-r" "/dev/null" "--no-backup-if-mismatch"
571 "-fl" o))))
572 (save-restriction
573 (narrow-to-region m0b m0e)
574 (smerge-remove-props m0b m0e)
575 (insert-file-contents o nil nil nil t)))
576 ;; Try "diff -b BASE OTHER | patch MINE".
577 ((when (and (not safe) m2e b
578 ;; If the BASE is empty, this would just concatenate
579 ;; the two, which is rarely right.
580 (not (eq m2b m2e)))
581 (write-region m3b m3e o nil 'silent)
582 (call-process diff-command nil buf nil "-bc" b o)
583 (with-current-buffer buf
584 (zerop (call-process-region
585 (point-min) (point-max) "patch" t nil nil
586 "-r" "/dev/null" "--no-backup-if-mismatch"
587 "-fl" m))))
588 (save-restriction
589 (narrow-to-region m0b m0e)
590 (smerge-remove-props m0b m0e)
591 (insert-file-contents m nil nil nil t)))
592 ;; If the conflict is only made of comments, and one of the two
593 ;; changes is only rearranging spaces (e.g. reflowing text) while
594 ;; the other is a real change, drop the space-rearrangement.
595 ((and m2e
596 (comment-only-p m1b m1e)
597 (comment-only-p m2b m2e)
598 (comment-only-p m3b m3e)
599 (let ((t1 (smerge-resolve--extract-comment m1b m1e))
600 (t2 (smerge-resolve--extract-comment m2b m2e))
601 (t3 (smerge-resolve--extract-comment m3b m3e)))
602 (cond
603 ((and (equal t1 t2) (not (equal t2 t3)))
604 (setq choice 3))
605 ((and (not (equal t1 t2)) (equal t2 t3))
606 (setq choice 1)))))
607 (set-match-data md)
608 (smerge-keep-n choice))
609 ;; Idem, when the conflict is contained within a single comment.
610 ((save-excursion
611 (and m2e
612 (nth 4 (syntax-ppss m0b))
613 ;; If there's a conflict earlier in the file,
614 ;; syntax-ppss is not reliable.
615 (not (re-search-backward smerge-begin-re nil t))
616 (progn (goto-char (nth 8 (syntax-ppss m0b)))
617 (forward-comment 1)
618 (> (point) m0e))
619 (let ((t1 (smerge-resolve--normalize m1b m1e))
620 (t2 (smerge-resolve--normalize m2b m2e))
621 (t3 (smerge-resolve--normalize m3b m3e)))
622 (cond
623 ((and (equal t1 t2) (not (equal t2 t3)))
624 (setq choice 3))
625 ((and (not (equal t1 t2)) (equal t2 t3))
626 (setq choice 1))))))
627 (set-match-data md)
628 (smerge-keep-n choice))
629 (t
630 (error "Don't know how to resolve"))))
631 (if (buffer-name buf) (kill-buffer buf))
632 (if m (delete-file m))
633 (if b (delete-file b))
634 (if o (delete-file o))))
635 (smerge-auto-leave))
636
637 (defun smerge-resolve-all ()
638 "Perform automatic resolution on all conflicts."
639 (interactive)
640 (save-excursion
641 (goto-char (point-min))
642 (while (re-search-forward smerge-begin-re nil t)
643 (condition-case nil
644 (progn
645 (smerge-match-conflict)
646 (smerge-resolve 'safe))
647 (error nil)))))
648
649 (defun smerge-batch-resolve ()
650 ;; command-line-args-left is what is left of the command line.
651 (if (not noninteractive)
652 (error "`smerge-batch-resolve' is to be used only with -batch"))
653 (while command-line-args-left
654 (let ((file (pop command-line-args-left)))
655 (if (string-match "\\.rej\\'" file)
656 ;; .rej files should never contain diff3 markers, on the other hand,
657 ;; in Arch, .rej files are sometimes used to indicate that the
658 ;; main file has diff3 markers. So you can pass **/*.rej and
659 ;; it will DTRT.
660 (setq file (substring file 0 (match-beginning 0))))
661 (message "Resolving conflicts in %s..." file)
662 (when (file-readable-p file)
663 (with-current-buffer (find-file-noselect file)
664 (smerge-resolve-all)
665 (save-buffer)
666 (kill-buffer (current-buffer)))))))
667
668 (defun smerge-keep-base ()
669 "Revert to the base version."
670 (interactive)
671 (smerge-match-conflict)
672 (smerge-ensure-match 2)
673 (smerge-keep-n 2)
674 (smerge-auto-leave))
675
676 (defun smerge-keep-other ()
677 "Use \"other\" version."
678 (interactive)
679 (smerge-match-conflict)
680 ;;(smerge-ensure-match 3)
681 (smerge-keep-n 3)
682 (smerge-auto-leave))
683
684 (defun smerge-keep-mine ()
685 "Keep your version."
686 (interactive)
687 (smerge-match-conflict)
688 ;;(smerge-ensure-match 1)
689 (smerge-keep-n 1)
690 (smerge-auto-leave))
691
692 (defun smerge-get-current ()
693 (let ((i 3))
694 (while (or (not (match-end i))
695 (< (point) (match-beginning i))
696 (>= (point) (match-end i)))
697 (decf i))
698 i))
699
700 (defun smerge-keep-current ()
701 "Use the current (under the cursor) version."
702 (interactive)
703 (smerge-match-conflict)
704 (let ((i (smerge-get-current)))
705 (if (<= i 0) (error "Not inside a version")
706 (smerge-keep-n i)
707 (smerge-auto-leave))))
708
709 (defun smerge-kill-current ()
710 "Remove the current (under the cursor) version."
711 (interactive)
712 (smerge-match-conflict)
713 (let ((i (smerge-get-current)))
714 (if (<= i 0) (error "Not inside a version")
715 (let ((left nil))
716 (dolist (n '(3 2 1))
717 (if (and (match-end n) (/= (match-end n) (match-end i)))
718 (push n left)))
719 (if (and (cdr left)
720 (/= (match-end (car left)) (match-end (cadr left))))
721 (ding) ;We don't know how to do that.
722 (smerge-keep-n (car left))
723 (smerge-auto-leave))))))
724
725 (defun smerge-diff-base-mine ()
726 "Diff 'base' and 'mine' version in current conflict region."
727 (interactive)
728 (smerge-diff 2 1))
729
730 (defun smerge-diff-base-other ()
731 "Diff 'base' and 'other' version in current conflict region."
732 (interactive)
733 (smerge-diff 2 3))
734
735 (defun smerge-diff-mine-other ()
736 "Diff 'mine' and 'other' version in current conflict region."
737 (interactive)
738 (smerge-diff 1 3))
739
740 (defun smerge-match-conflict ()
741 "Get info about the conflict. Puts the info in the `match-data'.
742 The submatches contain:
743 0: the whole conflict.
744 1: your code.
745 2: the base code.
746 3: other code.
747 An error is raised if not inside a conflict."
748 (save-excursion
749 (condition-case nil
750 (let* ((orig-point (point))
751
752 (_ (forward-line 1))
753 (_ (re-search-backward smerge-begin-re))
754
755 (start (match-beginning 0))
756 (mine-start (match-end 0))
757 (filename (or (match-string 1) ""))
758
759 (_ (re-search-forward smerge-end-re))
760 (_ (assert (< orig-point (match-end 0))))
761
762 (other-end (match-beginning 0))
763 (end (match-end 0))
764
765 (_ (re-search-backward smerge-other-re start))
766
767 (mine-end (match-beginning 0))
768 (other-start (match-end 0))
769
770 base-start base-end)
771
772 ;; handle the various conflict styles
773 (cond
774 ((save-excursion
775 (goto-char mine-start)
776 (re-search-forward smerge-begin-re end t))
777 ;; There's a nested conflict and we're after the beginning
778 ;; of the outer one but before the beginning of the inner one.
779 ;; Of course, maybe this is not a nested conflict but in that
780 ;; case it can only be something nastier that we don't know how
781 ;; to handle, so may as well arbitrarily decide to treat it as
782 ;; a nested conflict. --Stef
783 (error "There is a nested conflict"))
784
785 ((re-search-backward smerge-base-re start t)
786 ;; a 3-parts conflict
787 (set (make-local-variable 'smerge-conflict-style) 'diff3-A)
788 (setq base-end mine-end)
789 (setq mine-end (match-beginning 0))
790 (setq base-start (match-end 0)))
791
792 ((string= filename (file-name-nondirectory
793 (or buffer-file-name "")))
794 ;; a 2-parts conflict
795 (set (make-local-variable 'smerge-conflict-style) 'diff3-E))
796
797 ((and (not base-start)
798 (or (eq smerge-conflict-style 'diff3-A)
799 (equal filename "ANCESTOR")
800 (string-match "\\`[.0-9]+\\'" filename)))
801 ;; a same-diff conflict
802 (setq base-start mine-start)
803 (setq base-end mine-end)
804 (setq mine-start other-start)
805 (setq mine-end other-end)))
806
807 (store-match-data (list start end
808 mine-start mine-end
809 base-start base-end
810 other-start other-end
811 (when base-start (1- base-start)) base-start
812 (1- other-start) other-start))
813 t)
814 (search-failed (error "Point not in conflict region")))))
815
816 (add-to-list 'debug-ignored-errors "Point not in conflict region")
817
818 (defun smerge-conflict-overlay (pos)
819 "Return the conflict overlay at POS if any."
820 (let ((ols (overlays-at pos))
821 conflict)
822 (dolist (ol ols)
823 (if (and (eq (overlay-get ol 'smerge) 'conflict)
824 (> (overlay-end ol) pos))
825 (setq conflict ol)))
826 conflict))
827
828 (defun smerge-find-conflict (&optional limit)
829 "Find and match a conflict region. Intended as a font-lock MATCHER.
830 The submatches are the same as in `smerge-match-conflict'.
831 Returns non-nil if a match is found between point and LIMIT.
832 Point is moved to the end of the conflict."
833 (let ((found nil)
834 (pos (point))
835 conflict)
836 ;; First check to see if point is already inside a conflict, using
837 ;; the conflict overlays.
838 (while (and (not found) (setq conflict (smerge-conflict-overlay pos)))
839 ;; Check the overlay's validity and kill it if it's out of date.
840 (condition-case nil
841 (progn
842 (goto-char (overlay-start conflict))
843 (smerge-match-conflict)
844 (goto-char (match-end 0))
845 (if (<= (point) pos)
846 (error "Matching backward!")
847 (setq found t)))
848 (error (smerge-remove-props
849 (overlay-start conflict) (overlay-end conflict))
850 (goto-char pos))))
851 ;; If we're not already inside a conflict, look for the next conflict
852 ;; and add/update its overlay.
853 (while (and (not found) (re-search-forward smerge-begin-re limit t))
854 (condition-case nil
855 (progn
856 (smerge-match-conflict)
857 (goto-char (match-end 0))
858 (let ((conflict (smerge-conflict-overlay (1- (point)))))
859 (if conflict
860 ;; Update its location, just in case it got messed up.
861 (move-overlay conflict (match-beginning 0) (match-end 0))
862 (setq conflict (make-overlay (match-beginning 0) (match-end 0)
863 nil 'front-advance nil))
864 (overlay-put conflict 'evaporate t)
865 (overlay-put conflict 'smerge 'conflict)
866 (let ((props smerge-text-properties))
867 (while props
868 (overlay-put conflict (pop props) (pop props))))))
869 (setq found t))
870 (error nil)))
871 found))
872
873 ;;; Refined change highlighting
874
875 (defvar smerge-refine-forward-function 'smerge-refine-forward
876 "Function used to determine an \"atomic\" element.
877 You can set it to `forward-char' to get char-level granularity.
878 Its behavior has mainly two restrictions:
879 - if this function encounters a newline, it's important that it stops right
880 after the newline.
881 This only matters if `smerge-refine-ignore-whitespace' is nil.
882 - it needs to be unaffected by changes performed by the `preproc' argument
883 to `smerge-refine-subst'.
884 This only matters if `smerge-refine-weight-hack' is nil.")
885
886 (defvar smerge-refine-ignore-whitespace t
887 "If non-nil, indicate that `smerge-refine' should try to ignore change in whitespace.")
888
889 (defvar smerge-refine-weight-hack t
890 "If non-nil, pass to diff as many lines as there are chars in the region.
891 I.e. each atomic element (e.g. word) will be copied as many times (on different
892 lines) as it has chars. This has two advantages:
893 - if `diff' tries to minimize the number *lines* (rather than chars)
894 added/removed, this adjust the weights so that adding/removing long
895 symbols is considered correspondingly more costly.
896 - `smerge-refine-forward-function' only needs to be called when chopping up
897 the regions, and `forward-char' can be used afterwards.
898 It has the following disadvantages:
899 - cannot use `diff -w' because the weighting causes added spaces in a line
900 to be represented as added copies of some line, so `diff -w' can't do the
901 right thing any more.
902 - may in degenerate cases take a 1KB input region and turn it into a 1MB
903 file to pass to diff.")
904
905 (defun smerge-refine-forward (n)
906 (let ((case-fold-search nil)
907 (re "[[:upper:]]?[[:lower:]]+\\|[[:upper:]]+\\|[[:digit:]]+\\|.\\|\n"))
908 (when (and smerge-refine-ignore-whitespace
909 ;; smerge-refine-weight-hack causes additional spaces to
910 ;; appear as additional lines as well, so even if diff ignore
911 ;; whitespace changes, it'll report added/removed lines :-(
912 (not smerge-refine-weight-hack))
913 (setq re (concat "[ \t]*\\(?:" re "\\)")))
914 (dotimes (i n)
915 (unless (looking-at re) (error "Smerge refine internal error"))
916 (goto-char (match-end 0)))))
917
918 (defun smerge-refine-chopup-region (beg end file &optional preproc)
919 "Chopup the region into small elements, one per line.
920 Save the result into FILE.
921 If non-nil, PREPROC is called with no argument in a buffer that contains
922 a copy of the text, just before chopping it up. It can be used to replace
923 chars to try and eliminate some spurious differences."
924 ;; We used to chop up char-by-char rather than word-by-word like ediff
925 ;; does. It had the benefit of simplicity and very fine results, but it
926 ;; often suffered from problem that diff would find correlations where
927 ;; there aren't any, so the resulting "change" didn't make much sense.
928 ;; You can still get this behavior by setting
929 ;; `smerge-refine-forward-function' to `forward-char'.
930 (let ((buf (current-buffer)))
931 (with-temp-buffer
932 (insert-buffer-substring buf beg end)
933 (when preproc (goto-char (point-min)) (funcall preproc))
934 (when smerge-refine-ignore-whitespace
935 ;; It doesn't make much of a difference for diff-fine-highlight
936 ;; because we still have the _/+/</>/! prefix anyway. Can still be
937 ;; useful in other circumstances.
938 (subst-char-in-region (point-min) (point-max) ?\n ?\s))
939 (goto-char (point-min))
940 (while (not (eobp))
941 (funcall smerge-refine-forward-function 1)
942 (let ((s (if (prog2 (forward-char -1) (bolp) (forward-char 1))
943 nil
944 (buffer-substring (line-beginning-position) (point)))))
945 ;; We add \n after each char except after \n, so we get
946 ;; one line per text char, where each line contains
947 ;; just one char, except for \n chars which are
948 ;; represented by the empty line.
949 (unless (eq (char-before) ?\n) (insert ?\n))
950 ;; HACK ALERT!!
951 (if smerge-refine-weight-hack
952 (dotimes (i (1- (length s))) (insert s "\n")))))
953 (unless (bolp) (error "Smerge refine internal error"))
954 (let ((coding-system-for-write 'emacs-mule))
955 (write-region (point-min) (point-max) file nil 'nomessage)))))
956
957 (defun smerge-refine-highlight-change (buf beg match-num1 match-num2 props)
958 (with-current-buffer buf
959 (goto-char beg)
960 (let* ((startline (- (string-to-number match-num1) 1))
961 (beg (progn (funcall (if smerge-refine-weight-hack
962 'forward-char
963 smerge-refine-forward-function)
964 startline)
965 (point)))
966 (end (progn (funcall (if smerge-refine-weight-hack
967 'forward-char
968 smerge-refine-forward-function)
969 (if match-num2
970 (- (string-to-number match-num2)
971 startline)
972 1))
973 (point))))
974 (when smerge-refine-ignore-whitespace
975 (skip-chars-backward " \t\n" beg) (setq end (point))
976 (goto-char beg)
977 (skip-chars-forward " \t\n" end) (setq beg (point)))
978 (when (> end beg)
979 (let ((ol (make-overlay
980 beg end nil
981 ;; Make them tend to shrink rather than spread when editing.
982 'front-advance nil)))
983 (overlay-put ol 'evaporate t)
984 (dolist (x props) (overlay-put ol (car x) (cdr x)))
985 ol)))))
986
987 (defun smerge-refine-subst (beg1 end1 beg2 end2 props &optional preproc)
988 "Show fine differences in the two regions BEG1..END1 and BEG2..END2.
989 PROPS is an alist of properties to put (via overlays) on the changes.
990 If non-nil, PREPROC is called with no argument in a buffer that contains
991 a copy of a region, just before preparing it to for `diff'. It can be
992 used to replace chars to try and eliminate some spurious differences."
993 (let* ((buf (current-buffer))
994 (pos (point))
995 (file1 (make-temp-file "diff1"))
996 (file2 (make-temp-file "diff2")))
997 ;; Chop up regions into smaller elements and save into files.
998 (smerge-refine-chopup-region beg1 end1 file1 preproc)
999 (smerge-refine-chopup-region beg2 end2 file2 preproc)
1000
1001 ;; Call diff on those files.
1002 (unwind-protect
1003 (with-temp-buffer
1004 (let ((coding-system-for-read 'emacs-mule))
1005 (call-process diff-command nil t nil
1006 (if (and smerge-refine-ignore-whitespace
1007 (not smerge-refine-weight-hack))
1008 ;; Pass -a so diff treats it as a text file even
1009 ;; if it contains \0 and such.
1010 ;; Pass -d so as to get the smallest change, but
1011 ;; also and more importantly because otherwise it
1012 ;; may happen that diff doesn't behave like
1013 ;; smerge-refine-weight-hack expects it to.
1014 ;; See http://thread.gmane.org/gmane.emacs.devel/82685.
1015 "-awd" "-ad")
1016 file1 file2))
1017 ;; Process diff's output.
1018 (goto-char (point-min))
1019 (let ((last1 nil)
1020 (last2 nil))
1021 (while (not (eobp))
1022 (if (not (looking-at "\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?\\([acd]\\)\\([0-9]+\\)\\(?:,\\([0-9]+\\)\\)?$"))
1023 (error "Unexpected patch hunk header: %s"
1024 (buffer-substring (point) (line-end-position))))
1025 (let ((op (char-after (match-beginning 3)))
1026 (m1 (match-string 1))
1027 (m2 (match-string 2))
1028 (m4 (match-string 4))
1029 (m5 (match-string 5)))
1030 (when (memq op '(?d ?c))
1031 (setq last1
1032 (smerge-refine-highlight-change buf beg1 m1 m2 props)))
1033 (when (memq op '(?a ?c))
1034 (setq last2
1035 (smerge-refine-highlight-change buf beg2 m4 m5 props))))
1036 (forward-line 1) ;Skip hunk header.
1037 (and (re-search-forward "^[0-9]" nil 'move) ;Skip hunk body.
1038 (goto-char (match-beginning 0))))
1039 ;; (assert (or (null last1) (< (overlay-start last1) end1)))
1040 ;; (assert (or (null last2) (< (overlay-start last2) end2)))
1041 (if smerge-refine-weight-hack
1042 (progn
1043 ;; (assert (or (null last1) (<= (overlay-end last1) end1)))
1044 ;; (assert (or (null last2) (<= (overlay-end last2) end2)))
1045 )
1046 ;; smerge-refine-forward-function when calling in chopup may
1047 ;; have stopped because it bumped into EOB whereas in
1048 ;; smerge-refine-weight-hack it may go a bit further.
1049 (if (and last1 (> (overlay-end last1) end1))
1050 (move-overlay last1 (overlay-start last1) end1))
1051 (if (and last2 (> (overlay-end last2) end2))
1052 (move-overlay last2 (overlay-start last2) end2))
1053 )))
1054 (goto-char pos)
1055 (delete-file file1)
1056 (delete-file file2))))
1057
1058 (defun smerge-refine (&optional part)
1059 "Highlight the words of the conflict that are different.
1060 For 3-way conflicts, highlights only two of the three parts.
1061 A numeric argument PART can be used to specify which two parts;
1062 repeating the command will highlight other two parts."
1063 (interactive
1064 (if (integerp current-prefix-arg) (list current-prefix-arg)
1065 (smerge-match-conflict)
1066 (let* ((prop (get-text-property (match-beginning 0) 'smerge-refine-part))
1067 (part (if (and (consp prop)
1068 (eq (buffer-chars-modified-tick) (car prop)))
1069 (cdr prop))))
1070 ;; If already highlighted, cycle.
1071 (list (if (integerp part) (1+ (mod part 3)))))))
1072
1073 (if (and (integerp part) (or (< part 1) (> part 3)))
1074 (error "No conflict part nb %s" part))
1075 (smerge-match-conflict)
1076 (remove-overlays (match-beginning 0) (match-end 0) 'smerge 'refine)
1077 ;; Ignore `part' if not applicable, and default it if not provided.
1078 (setq part (cond ((null (match-end 2)) 2)
1079 ((eq (match-end 1) (match-end 3)) 1)
1080 ((integerp part) part)
1081 ;; If one of the parts is empty, any refinement using
1082 ;; it will be trivial and uninteresting.
1083 ((eq (match-end 1) (match-beginning 1)) 1)
1084 ((eq (match-end 3) (match-beginning 3)) 3)
1085 (t 2)))
1086 (let ((n1 (if (eq part 1) 2 1))
1087 (n2 (if (eq part 3) 2 3)))
1088 (smerge-ensure-match n1)
1089 (smerge-ensure-match n2)
1090 (with-silent-modifications
1091 (put-text-property (match-beginning 0) (1+ (match-beginning 0))
1092 'smerge-refine-part
1093 (cons (buffer-chars-modified-tick) part)))
1094 (smerge-refine-subst (match-beginning n1) (match-end n1)
1095 (match-beginning n2) (match-end n2)
1096 '((smerge . refine)
1097 (face . smerge-refined-change)))))
1098
1099 (defun smerge-diff (n1 n2)
1100 (smerge-match-conflict)
1101 (smerge-ensure-match n1)
1102 (smerge-ensure-match n2)
1103 (let ((name1 (aref smerge-match-names n1))
1104 (name2 (aref smerge-match-names n2))
1105 ;; Read them before the match-data gets clobbered.
1106 (beg1 (match-beginning n1))
1107 (end1 (match-end n1))
1108 (beg2 (match-beginning n2))
1109 (end2 (match-end n2))
1110 (file1 (make-temp-file "smerge1"))
1111 (file2 (make-temp-file "smerge2"))
1112 (dir default-directory)
1113 (file (if buffer-file-name (file-relative-name buffer-file-name)))
1114 ;; We would want to use `emacs-mule-unix' for read&write, but we
1115 ;; bump into problems with the coding-system used by diff to write
1116 ;; the file names and the time stamps in the header.
1117 ;; `buffer-file-coding-system' is not always correct either, but if
1118 ;; the OS/user uses only one coding-system, then it works.
1119 (coding-system-for-read buffer-file-coding-system))
1120 (write-region beg1 end1 file1 nil 'nomessage)
1121 (write-region beg2 end2 file2 nil 'nomessage)
1122 (unwind-protect
1123 (with-current-buffer (get-buffer-create smerge-diff-buffer-name)
1124 (setq default-directory dir)
1125 (let ((inhibit-read-only t))
1126 (erase-buffer)
1127 (let ((status
1128 (apply 'call-process diff-command nil t nil
1129 (append smerge-diff-switches
1130 (list "-L" (concat name1 "/" file)
1131 "-L" (concat name2 "/" file)
1132 file1 file2)))))
1133 (if (eq status 0) (insert "No differences found.\n"))))
1134 (goto-char (point-min))
1135 (diff-mode)
1136 (display-buffer (current-buffer) t))
1137 (delete-file file1)
1138 (delete-file file2))))
1139
1140 ;; compiler pacifiers
1141 (defvar smerge-ediff-windows)
1142 (defvar smerge-ediff-buf)
1143 (defvar ediff-buffer-A)
1144 (defvar ediff-buffer-B)
1145 (defvar ediff-buffer-C)
1146 (defvar ediff-ancestor-buffer)
1147 (defvar ediff-quit-hook)
1148 (declare-function ediff-cleanup-mess "ediff-util" nil)
1149
1150 ;;;###autoload
1151 (defun smerge-ediff (&optional name-mine name-other name-base)
1152 "Invoke ediff to resolve the conflicts.
1153 NAME-MINE, NAME-OTHER, and NAME-BASE, if non-nil, are used for the
1154 buffer names."
1155 (interactive)
1156 (let* ((buf (current-buffer))
1157 (mode major-mode)
1158 ;;(ediff-default-variant 'default-B)
1159 (config (current-window-configuration))
1160 (filename (file-name-nondirectory buffer-file-name))
1161 (mine (generate-new-buffer
1162 (or name-mine (concat "*" filename " MINE*"))))
1163 (other (generate-new-buffer
1164 (or name-other (concat "*" filename " OTHER*"))))
1165 base)
1166 (with-current-buffer mine
1167 (buffer-disable-undo)
1168 (insert-buffer-substring buf)
1169 (goto-char (point-min))
1170 (while (smerge-find-conflict)
1171 (when (match-beginning 2) (setq base t))
1172 (smerge-keep-n 1))
1173 (buffer-enable-undo)
1174 (set-buffer-modified-p nil)
1175 (funcall mode))
1176
1177 (with-current-buffer other
1178 (buffer-disable-undo)
1179 (insert-buffer-substring buf)
1180 (goto-char (point-min))
1181 (while (smerge-find-conflict)
1182 (smerge-keep-n 3))
1183 (buffer-enable-undo)
1184 (set-buffer-modified-p nil)
1185 (funcall mode))
1186
1187 (when base
1188 (setq base (generate-new-buffer
1189 (or name-base (concat "*" filename " BASE*"))))
1190 (with-current-buffer base
1191 (buffer-disable-undo)
1192 (insert-buffer-substring buf)
1193 (goto-char (point-min))
1194 (while (smerge-find-conflict)
1195 (if (match-end 2)
1196 (smerge-keep-n 2)
1197 (delete-region (match-beginning 0) (match-end 0))))
1198 (buffer-enable-undo)
1199 (set-buffer-modified-p nil)
1200 (funcall mode)))
1201
1202 ;; the rest of the code is inspired from vc.el
1203 ;; Fire up ediff.
1204 (set-buffer
1205 (if base
1206 (ediff-merge-buffers-with-ancestor mine other base)
1207 ;; nil 'ediff-merge-revisions-with-ancestor buffer-file-name)
1208 (ediff-merge-buffers mine other)))
1209 ;; nil 'ediff-merge-revisions buffer-file-name)))
1210
1211 ;; Ediff is now set up, and we are in the control buffer.
1212 ;; Do a few further adjustments and take precautions for exit.
1213 (set (make-local-variable 'smerge-ediff-windows) config)
1214 (set (make-local-variable 'smerge-ediff-buf) buf)
1215 (set (make-local-variable 'ediff-quit-hook)
1216 (lambda ()
1217 (let ((buffer-A ediff-buffer-A)
1218 (buffer-B ediff-buffer-B)
1219 (buffer-C ediff-buffer-C)
1220 (buffer-Ancestor ediff-ancestor-buffer)
1221 (buf smerge-ediff-buf)
1222 (windows smerge-ediff-windows))
1223 (ediff-cleanup-mess)
1224 (with-current-buffer buf
1225 (erase-buffer)
1226 (insert-buffer-substring buffer-C)
1227 (kill-buffer buffer-A)
1228 (kill-buffer buffer-B)
1229 (kill-buffer buffer-C)
1230 (when (bufferp buffer-Ancestor) (kill-buffer buffer-Ancestor))
1231 (set-window-configuration windows)
1232 (message "Conflict resolution finished; you may save the buffer")))))
1233 (message "Please resolve conflicts now; exit ediff when done")))
1234
1235 (defun smerge-makeup-conflict (pt1 pt2 pt3 &optional pt4)
1236 "Insert diff3 markers to make a new conflict.
1237 Uses point and mark for two of the relevant positions and previous marks
1238 for the other ones.
1239 By default, makes up a 2-way conflict,
1240 with a \\[universal-argument] prefix, makes up a 3-way conflict."
1241 (interactive
1242 (list (point)
1243 (mark)
1244 (progn (pop-mark) (mark))
1245 (when current-prefix-arg (pop-mark) (mark))))
1246 ;; Start from the end so as to avoid problems with pos-changes.
1247 (destructuring-bind (pt1 pt2 pt3 &optional pt4)
1248 (sort (list* pt1 pt2 pt3 (if pt4 (list pt4))) '>=)
1249 (goto-char pt1) (beginning-of-line)
1250 (insert ">>>>>>> OTHER\n")
1251 (goto-char pt2) (beginning-of-line)
1252 (insert "=======\n")
1253 (goto-char pt3) (beginning-of-line)
1254 (when pt4
1255 (insert "||||||| BASE\n")
1256 (goto-char pt4) (beginning-of-line))
1257 (insert "<<<<<<< MINE\n"))
1258 (if smerge-mode nil (smerge-mode 1))
1259 (smerge-refine))
1260
1261
1262 (defconst smerge-parsep-re
1263 (concat smerge-begin-re "\\|" smerge-end-re "\\|"
1264 smerge-base-re "\\|" smerge-other-re "\\|"))
1265
1266 ;;;###autoload
1267 (define-minor-mode smerge-mode
1268 "Minor mode to simplify editing output from the diff3 program.
1269 \\{smerge-mode-map}"
1270 :group 'smerge :lighter " SMerge"
1271 (when (and (boundp 'font-lock-mode) font-lock-mode)
1272 (save-excursion
1273 (if smerge-mode
1274 (font-lock-add-keywords nil smerge-font-lock-keywords 'append)
1275 (font-lock-remove-keywords nil smerge-font-lock-keywords))
1276 (goto-char (point-min))
1277 (while (smerge-find-conflict)
1278 (save-excursion
1279 (font-lock-fontify-region (match-beginning 0) (match-end 0) nil)))))
1280 (if (string-match (regexp-quote smerge-parsep-re) paragraph-separate)
1281 (unless smerge-mode
1282 (set (make-local-variable 'paragraph-separate)
1283 (replace-match "" t t paragraph-separate)))
1284 (when smerge-mode
1285 (set (make-local-variable 'paragraph-separate)
1286 (concat smerge-parsep-re paragraph-separate))))
1287 (unless smerge-mode
1288 (smerge-remove-props (point-min) (point-max))))
1289
1290 ;;;###autoload
1291 (defun smerge-start-session ()
1292 "Turn on `smerge-mode' and move point to first conflict marker.
1293 If no conflict maker is found, turn off `smerge-mode'."
1294 (interactive)
1295 (smerge-mode 1)
1296 (condition-case nil
1297 (unless (looking-at smerge-begin-re)
1298 (smerge-next))
1299 (error (smerge-auto-leave))))
1300
1301 (provide 'smerge-mode)
1302
1303 ;;; smerge-mode.el ends here