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