(Glossary): Treat Transient Mark mode as the default.
[bpt/emacs.git] / lisp / smerge-mode.el
index cd5c7c2..8a3f15b 100644 (file)
@@ -1,7 +1,7 @@
 ;;; smerge-mode.el --- Minor mode to resolve diff3 conflicts
 
 ;; Copyright (C) 1999, 2000, 2001, 2002, 2003,
-;;   2004, 2005, 2006, 2007 Free Software Foundation, Inc.
+;;   2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
 
 ;; Author: Stefan Monnier <monnier@iro.umontreal.ca>
 ;; Keywords: tools revision-control merge diff3 cvs conflict
@@ -297,6 +297,8 @@ Can be nil if the style is undecided, or else:
 
 (defun smerge-combine-with-next ()
   "Combine the current conflict with the next one."
+  ;; `smerge-auto-combine' relies on the finish position (at the beginning
+  ;; of the closing marker).
   (interactive)
   (smerge-match-conflict)
   (let ((ends nil))
@@ -328,6 +330,25 @@ Can be nil if the style is undecided, or else:
        (dolist (m match-data) (if m (move-marker m nil)))
        (mapc (lambda (m) (if m (move-marker m nil))) ends)))))
 
+(defvar smerge-auto-combine-max-separation 2
+  "Max number of lines between conflicts that should be combined.")
+
+(defun smerge-auto-combine ()
+  "Automatically combine conflicts that are near each other."
+  (interactive)
+  (save-excursion
+    (goto-char (point-min))
+    (while (smerge-find-conflict)
+      ;; 2 is 1 (default) + 1 (the begin markers).
+      (while (save-excursion
+               (smerge-find-conflict
+                (line-beginning-position
+                 (+ 2 smerge-auto-combine-max-separation))))
+        (forward-line -1)               ;Go back inside the conflict.
+        (smerge-combine-with-next)
+        (forward-line 1)                ;Move past the end of the conflict.
+        ))))
+
 (defvar smerge-resolve-function
   (lambda () (error "Don't know how to resolve"))
   "Mode-specific merge function.
@@ -790,12 +811,17 @@ replace chars to try and eliminate some spurious differences."
     (unwind-protect
         (with-temp-buffer
           (let ((coding-system-for-read 'emacs-mule))
-            ;; Don't forget -a to make sure diff treats it as a text file
-            ;; even if it contains \0 and such.
             (call-process diff-command nil t nil
                           (if (and smerge-refine-ignore-whitespace
                                    (not smerge-refine-weight-hack))
-                              "-aw" "-a")
+                              ;; Pass -a so diff treats it as a text file even
+                              ;; if it contains \0 and such.
+                              ;; Pass -d so as to get the smallest change, but
+                              ;; also and more importantly because otherwise it
+                              ;; may happen that diff doesn't behave like
+                              ;; smerge-refine-weight-hack expects it to.
+                              ;; See http://thread.gmane.org/gmane.emacs.devel/82685.
+                              "-awd" "-ad")
                           file1 file2))
           ;; Process diff's output.
           (goto-char (point-min))
@@ -846,10 +872,12 @@ replace chars to try and eliminate some spurious differences."
   (remove-overlays (match-beginning 0) (match-end 0) 'smerge 'refine)
   (smerge-ensure-match 1)
   (smerge-ensure-match 3)
-  (smerge-refine-subst (match-beginning 1) (match-end 1)
-                       (match-beginning 3) (match-end 3)
-                       '((smerge . refine)
-                         (face . smerge-refined-change))))
+  ;; Match 1 and 3 may be one and the same in case of trivial diff3 -A conflict.
+  (let ((n1 (if (eq (match-end 1) (match-end 3)) 2 1)))
+    (smerge-refine-subst (match-beginning n1) (match-end n1)
+                         (match-beginning 3)  (match-end 3)
+                         '((smerge . refine)
+                           (face . smerge-refined-change)))))
 
 (defun smerge-diff (n1 n2)
   (smerge-match-conflict)
@@ -900,6 +928,7 @@ replace chars to try and eliminate some spurious differences."
 (defvar ediff-buffer-C)
 (defvar ediff-ancestor-buffer)
 (defvar ediff-quit-hook)
+(declare-function ediff-cleanup-mess "ediff-util" nil)
 
 ;;;###autoload
 (defun smerge-ediff (&optional name-mine name-other name-base)
@@ -986,6 +1015,32 @@ buffer names."
               (message "Conflict resolution finished; you may save the buffer")))))
     (message "Please resolve conflicts now; exit ediff when done")))
 
+(defun smerge-makeup-conflict (pt1 pt2 pt3 &optional pt4)
+  "Insert diff3 markers to make a new conflict.
+Uses point and mark for 2 of the relevant positions and previous marks
+for the other ones.
+By default, makes up a 2-way conflict,
+with a \\[universal-argument] prefix, makes up a 3-way conflict."
+  (interactive
+   (list (point)
+         (mark)
+         (progn (pop-mark) (mark))
+         (when current-prefix-arg (pop-mark) (mark))))
+  ;; Start from the end so as to avoid problems with pos-changes.
+  (destructuring-bind (pt1 pt2 pt3 &optional pt4)
+      (sort (list* pt1 pt2 pt3 (if pt4 (list pt4))) '>=)
+    (goto-char pt1) (beginning-of-line)
+    (insert ">>>>>>> OTHER\n")
+    (goto-char pt2) (beginning-of-line)
+    (insert "=======\n")
+    (goto-char pt3) (beginning-of-line)
+    (when pt4
+      (insert "||||||| BASE\n")
+      (goto-char pt4) (beginning-of-line))
+    (insert "<<<<<<< MINE\n"))
+  (if smerge-mode nil (smerge-mode 1))
+  (smerge-refine))
+      
 
 (defconst smerge-parsep-re
   (concat smerge-begin-re "\\|" smerge-end-re "\\|"
@@ -1015,6 +1070,14 @@ buffer names."
   (unless smerge-mode
     (smerge-remove-props (point-min) (point-max))))
 
+;;;###autoload
+(defun smerge-start-session ()
+  "Turn on `smerge-mode' and move point to first conflict marker.
+If no conflict maker is found, turn off `smerge-mode'."
+  (smerge-mode 1)
+  (condition-case nil
+      (smerge-next)
+    (error (smerge-auto-leave))))
 
 (provide 'smerge-mode)